我如何获得日期差? [英] How can i get the Date Difference?
本文介绍了我如何获得日期差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在计算testapp中两个日期之间的差异.如果我不更改月份,它会很好,但是当我更改月份时,它将不起作用.请帮助我,这里是我正在使用的代码:-
I am calculating difference between two dates in my testapp. It works fine if I don't change the month but when i change the month it doesn't work. Please help me here is code I am using:-
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
//[df setDateStyle:NSDateFormatterMediumStyle];
df.dateFormat = @"YYYY-MM-DD";
NSDate *date1 = [df dateFromString:@"2011-04-30"];
NSDate *date2 = [df dateFromString:@"2011-05-03"];
NSLog(@"date1=%@",date1);
NSLog(@"date2=%@",date2);
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
int numberOfDays = secondsBetween / 86400;
NSLog(@"There are %d days in between the two dates.", numberOfDays);
输出为-27.我感到困惑.
output is -27. I am confuse.
请任何人帮助我解决此问题.
please anyone help me to fix this problem..
推荐答案
尝试一下
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
//[df setDateStyle:NSDateFormatterMediumStyle];
//df.dateFormat = @"yyyy-mm-dd";
[df setDateFormat:@"yyyy-MM-dd"];
NSDate *date1 = [df dateFromString:@"2011-04-30"];
NSDate *date2 = [df dateFromString:@"2011-05-03"];
NSLog(@"date1=%@",date1);
NSLog(@"date2=%@",date2);
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
int numberOfDays = secondsBetween / 86400;
NSLog(@"There are %d days in between the two dates.", numberOfDays);
调整时区以获取正确的日期...
Adjust the time zone to get the correct dates...
这篇关于我如何获得日期差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文