C编程中的日期比较 [英] Date Comparision in C programming
本文介绍了C编程中的日期比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我需要在纯C语言中进行日期比较.
一个是char Date_1 [8],另一个是#defined常量"DATE2"
我想使用上述2个日期中的最新日期,并将其存储到文件中.
有人可以帮我吗?
提前致谢. :-O
Hi
I need to do the date comparision in plain C.
one is the char Date_1[8] and other is #defined constant "DATE2"
I want to use most recent date from above 2 dates and store it into a file.
Can anybody help me out.
Thanks in advance. :-O
推荐答案
如果允许
将日期编码为"YYYYMMDD",
您可以使用:):
If it is allowed
to code the date as "YYYYMMDD",
you could use :) :
#define d2 "19001229"
...
{
...
char d1[8] = {''2'',''0'',''1'',''0'',''0'',''4'',''0'',''6''};
int iResult = memcmp(d1 /*address of date1*/,
d2 /*address of date2*/,
sizeof(d1) /*=8, length to compare*/);
}
感谢回复.
但是我的日期格式是dd/mm/yyyy
我如何比较它们?
比较年,月,日.
我想使用最近的日期. :-O
Thanks for reply.
But my date format is dd/mm/yyyy
How can i compare them?
compare year, month ,date.
I want to use the most recent date. :-O
保存和比较日期的另一种方法:):
Another solution to hold and compare the dates :) :
typedef struct sMydate {
WORD m_wYear;
BYTE m_byMonth;
BYTE m_byDay;
sMydate(WORD wYear, BYTE byMonth, BYTE byDay) {
m_wYear = wYear;
m_byMonth = byMonth;
m_byDay = byDay;
}
int Compare(const sMydate& sAnotherDate) {
LONG lThisDate = MAKELONG(MAKEWORD(m_byDay, m_byMonth), m_wYear);
LONG lAnotherDate = MAKELONG(MAKEWORD(sAnotherDate.m_byDay,
sAnotherDate.m_byMonth),
sAnotherDate.m_wYear);
return memcmp(&lThisDate, &lAnotherDate, sizeof(LONG));
}
} MYDATE, *LPMYDATE;
...
const MYDATE sConstDate(1900, 12, 29);
...
{
...
MYDATE sLocalDate(2010, 04, 06);
int iResult = sLocalDate.Compare(sConstDate);
...
}
;
这篇关于C编程中的日期比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文