我如何比较“今天”和“今天”? (NSDate) 到格式化字符串日期 (dd/MM/yyyy)?
我是 iOS 编程新手,一直在寻找一种将“今天”(NSDate)与格式化字符串日期(dd/MM/yyyy)进行比较的方法。
我在 NSDate 的 earlierDate
、laterDate
等方面找到了一些很好的答案,但代码似乎在我的项目中不起作用。
就是这样:
// [book quandd] is the string : 25/02/2011 (or whatever dd/MM/yyyy date)
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];
if ( [[dateFormat dateFromString:[book quandd]] isEqualToDate:today]) {
NSLog(@"Dates are equal");
}
if ( [[dateFormat dateFromString:[book quandd]] earlierDate:today]) {
NSLog(@"earlier");
}
if ( [[dateFormat dateFromString:[book quandd]] laterDate:today]) {
NSLog(@"later");
}
无论我做什么,无论 [book quandd]
中的日期是什么,控制台总是写“早”和“晚”,就好像它们同时比 今天。
这个“bug”从哪里来?
例如,是否有一些简单/更简单的方法可以将今天日期与2011年2月26日进行比较?
I'm new to iOS programming and I've been looking a lot for a way to compare "today" (an NSDate) to a formatted string date (dd/MM/yyyy).
I've found some good answers with the NSDate's earlierDate
, laterDate
etc. but the code doesn't seem to work in my project.
here it is :
// [book quandd] is the string : 25/02/2011 (or whatever dd/MM/yyyy date)
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];
if ( [[dateFormat dateFromString:[book quandd]] isEqualToDate:today]) {
NSLog(@"Dates are equal");
}
if ( [[dateFormat dateFromString:[book quandd]] earlierDate:today]) {
NSLog(@"earlier");
}
if ( [[dateFormat dateFromString:[book quandd]] laterDate:today]) {
NSLog(@"later");
}
Whatever I do and whatever the date in [book quandd]
, the console always writes "earlier" and "later", as if they were later and earlier at the same time than today.
Where is that "bug" coming from ?
Is there some easy/easier way to compare the today date with 26/02/2011 for example ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
NSDate 包含日期和时间。除非时间精确到毫秒(如果不是更精细),否则两个 NSDate 值不会相等。当你执行
[NSDate date]
时,你会得到“现在”的确切时间,而如果你使用 NSDateFormatter 将字符串转换为日期(没有时间值),则使用午夜。如果你想查看两个 NSDate 是否在同一天,你可以使用 NSCalendar & NSDateComponent。或者您可以“作弊”并将两个日期格式化为没有时间值的日期字符串,然后比较这些字符串。
An NSDate includes both date and time. No two NSDate values will be equal unless the times are identical down to the millisecond (if not finer). When you do
[NSDate date]
you get the exact time of "now", while if you use NSDateFormatter to convert a string to date (with no time value) then midnight is used.If you want to see if two NSDates fall on the same day you can use NSCalendar & NSDateComponents. Or you can "cheat" and format both dates to a date string with no time value and compare the strings.
使用
timeIntervalSinceNow:
获取与必要日期的差异。您还应该尝试获取它们之间的差异,然后转换为字符串。
Use
timeIntervalSinceNow:
to get the diff from the necessary date.You should also try to get the difference between them and then convert to a string.
用这个
use this
您需要使用引用的日期之一检查 previousDate 的返回值。例如: if([date1 previousDate:date2] == date1)
You need to check return value of the earlierDate with one of the dates referred. Ex: if([date1 earlierDate:date2] == date1)
您的“错误”发生是因为laterDate和earlyDate返回早于或晚于的日期对象。返回对象始终为非零,因此两个 if 语句的计算结果均为 true。使用它们的正确方法是这样的:
或者,我使用了 timeIntervalSinceDate,它返回第一个日期和第二个日期之间的秒数。
Your "bug" happens because laterDate and earlierDate return the date object that is either earlier or later. The return object is always nonnil, so both if statements evaluate to true. The correct way to use them is this way:
Alternatively, I've used timeIntervalSinceDate, which returns the seconds between the first date and the second date.