[英]Comparing two dates as char arrays in C
Bear with my guys this is my second stackoverflow question, please point me in the right direction if I'm doing this wrong 与我一起忍受这是我的第二个stackoverflow问题,如果我做错了,请指出正确的方向
I have two dates as chars. 我有两个约会字符。
I print out the dates: 我打印出日期:
printf("%s - %s\n",tmpPtr->date, currentDate);
And I have an if statement which is always executed 我有一个if语句,它总是执行
if(tmpPtr->date != currentDate) { // perhaps strcmp(), don't know
printf("Dates are not equal\n");
}
But this cannot be true because these are my results: 但这不能成立,因为这是我的结果:
27/12/2015 - 27/12/2015
Dates are not equal
27/12/2015 - 27/12/2015
Dates are not equal
28/12/2015 - 27/12/2015
Dates are not equal
29/12/2015 - 28/12/2015
Dates are not equal
29/12/2015 - 29/12/2015
Dates are not equal
29/12/2015 - 29/12/2015
Dates are not equal
30/12/2015 - 29/12/2015
Dates are not equal
31/12/2015 - 30/12/2015
Dates are not equal
31/12/2015 - 31/12/2015
Dates are not equal
This can't be true because some dates are equal? 这是不正确的,因为某些日期相等吗?
Am I comparing the strings correctly? 我是否正确比较字符串? Is it just comparing the memory allocation or something of the sort? 它只是比较内存分配还是某种比较?
In order to compare strings, use strcmp()
like this : 为了比较字符串,请像这样使用strcmp()
:
if (strcmp(tmpPtr->date,currentDate) != 0) {
printf("Dates are not equal\n");
}
If dates are in the same format which seems to be the case, then use strcmp
. 如果日期似乎是相同的格式,请使用strcmp
。 Otherwise parse them and compare year, month and date of one string to corresponding fields of the other. 否则,解析它们,并将一个字符串的年,月和日期与另一个字符串的相应字段进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.