比较两个日期在C不同的格式 [英] comparing two dates with different format in C

查看:147
本文介绍了比较两个日期在C不同的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要比较两个日期2010-08-12和2010-8-12,但strcmp的使用它失败,所以请提出任何内置的C函数或C函数来比较这些日期。

I want to compare two dates "2010-08-12" and "2010-8-12" but using strcmp it is failing so please suggest any inbuilt C function or any C function to compare these dates.

问候

推荐答案

您可以编写一个简单而直接的函数将日期转换为数字,它是通过使用的 与strtol

You can write a simple and straightforward function to convert a date to a number that is good enough for ordering purposes by using strtol:

int seq_day(char *date) {
    int y = strtol(date, &date, 10);
    int m = strtol(++date, &date, 10);
    int d = strtol(++date, &date, 10);
    return (y*12+m)*31+d;
}

呼吁两国日期上述功能,并比较 INT 的结果;返回值将是较早的日期较小。

Call the above function on both dates, and compare the int results; the returned value will be smaller for the earlier date.

这篇关于比较两个日期在C不同的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆