gpt4 book ai didi

c - 如何用C语言比较Windows中的日期

转载 作者:行者123 更新时间:2023-11-30 19:16:04 25 4
gpt4 key购买 nike

这里是我如何在 Linux 中执行此操作的示例,但在 Windows 中我没有 strptime任何人都可以帮我解决这个问题吗?

#include <stdio.h>
#include <time.h>
#include <sys/time.h>

time_t to_seconds(const char *date)
{
struct tm storage = {0, 0, 0, 0, 0, 0, 0, 0, 0};
char *p = NULL;
time_t retval = 0;

p = (char *) strptime(date, "%d-%b-%Y", &storage);
if (p == NULL)
{
retval = 0;
}
else
{
retval = mktime(&storage);
}
return retval;
}

int main(int argc, char** argv)
{
time_t d1 = to_seconds("16-Jun-2015");
time_t d2 = to_seconds("13-Jun-2015");
if(d1 > d2)
{
printf("date 1 > date 2");
}
}

最佳答案

如果您碰巧使用.Net,我建议:

1) 使用Convert()DateTime.Parse()

示例:

string date = "01/08/2008";
DateTime dt = Convert.ToDateTime(date);

2) 使用DateTime.Compare()比较两个日期时间值:

示例:

DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
int result = DateTime.Compare(date1, date2);
if (result < 0)
Console.WriteLine("is earlier than");
else if (result == 0)
Console.WriteLine("is the same time as";
else
Console.WriteLine("is later than";

关于c - 如何用C语言比较Windows中的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31093083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com