如何比较两个日期,返回天数
如何比较两个日期返回天数。 例如:缺席 X 天的杯赛。 看我的代码。
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"d MMMM,yyyy"];
NSDate *date1 = [df dateFromString:@"11-05-2010"];
NSDate *date2 = [df dateFromString:@"11-06-2010"];
NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
//int days = (int)interval / 30;
//int months = (interval - (months/30)) / 30;
NSString *timeDiff = [NSString stringWithFormat:@"%dMissing%d days of the Cup",date1,date2, fabs(interval)];
label.text = timeDiff; // output (Missing X days of the Cup)
how can I compare two dates return number of days.
Ex: Missing X days of the Cup.
look my code.
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"d MMMM,yyyy"];
NSDate *date1 = [df dateFromString:@"11-05-2010"];
NSDate *date2 = [df dateFromString:@"11-06-2010"];
NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
//int days = (int)interval / 30;
//int months = (interval - (months/30)) / 30;
NSString *timeDiff = [NSString stringWithFormat:@"%dMissing%d days of the Cup",date1,date2, fabs(interval)];
label.text = timeDiff; // output (Missing X days of the Cup)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
来自 Apple 的示例,基本上使用 NSCalendar:
From Apple's example, basically use an NSCalendar:
您可以使用下一个类别:
You can use next category:
需要调用此方法并仅设置我们想要以不同方式计算的 2 个日期。
Need to call this method and set 2 dates only which we want to calculate differently.
尝试此类别:
您可以通过添加导入来使用此类别:
并从代码中调用它:
Try this category:
You can use this category by adding an import:
And call it from your code: