查找 iPhone 中两个日期之间的总天数
我想查找两个日期之间的总天数。
例如,今天是 01-01-2011(DD-MM-YYYY),第二个日期是 (25-03-2011),我如何找到总天数?
NSDate *currentdate=[NSDate date];
NSLog(@"curretdate is ==%@",currentdate);
NSDateFormatter *tempFormatter1 = [[[NSDateFormatter alloc]init]autorelease];
[tempFormatter1 setDateFormat:@"dd-mm-YYYY hh:mm:ss"];
NSDate *toDate = [tempFormatter1 dateFromString:@"20-04-2011 09:00:00"];
NSLog(@"toDate ==%@",toDate);
I would like to find total number of days between two dates.
e.g. today is 01-01-2011(DD-MM-YYYY) and second date is (25-03-2011), how would I find the total number of days?
NSDate *currentdate=[NSDate date];
NSLog(@"curretdate is ==%@",currentdate);
NSDateFormatter *tempFormatter1 = [[[NSDateFormatter alloc]init]autorelease];
[tempFormatter1 setDateFormat:@"dd-mm-YYYY hh:mm:ss"];
NSDate *toDate = [tempFormatter1 dateFromString:@"20-04-2011 09:00:00"];
NSLog(@"toDate ==%@",toDate);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
在您的日期格式中,您设置错误,它是 dd-MM-yyyy HH:mm:ss 。可能这就是问题所在..你得到了错误的日期并且没有得到答案我发送了一些代码来获取日期差异。
编辑1:
我们可以使用以下函数找到日期差异:
来自 Abizern 的 ans。查找 NSDateComponent 的更多信息 <强>这里。
In your date Format tor u set wrong it`s be dd-MM-yyyy HH:mm:ss . may be that was the problem.. u get wrong date and not get answer i send litte bit code for get day diffrence.
Edit 1:
we can find date difference using following function :
from Abizern`s ans. find more infromation for NSDateComponent here.
希望它对你有用......
Hope It will work for you........
更简单的方法是:
结果是:
尝试使用和操作秒来计算时间差是一个坏主意。 Cocoa 提供了大量用于日历计算的类和方法,您应该尽可能多地使用它们。
A simpler way of doing it is:
And the results are:
Trying to use and manipulate seconds for calculating time differences is a bad idea. There are a whole load of classes and methods provided by Cocoa for Calendrical calculations and you should use them as much as possible.
试试这个
Try this
//尝试一下
//try it
查找开始日期到结束日期之间的日期数。
to find the number of dates between start date to end date.
请尝试这个。希望它能帮助你...
Please try this. Hope it helps you...