[英]Conversion of NSString into the NSDate is giving wrong value and format in iPhone
I want to compare two dates.For that i have to dates in two strings(fromDates and toDates). 我想比较两个日期。为此,我必须用两个字符串(fromDates和toDates)约会。
here fromDate is showing the correct value in console. 此处fromDate在控制台中显示正确的值。
//Console.
19/12/2012 from date is
After the NSDateFormatter process im getting wrong format and date and sometime it is showing NULL. NSDateFormatter处理后,我得到了错误的格式和日期,有时显示为NULL。
//console
First = (null)
I used the below method. 我使用以下方法。
if (fromDate) {
self.printFromDate.text = [NSString stringWithFormat:@"%@",curBtn.dateString];
fromDates = self.printFromDate.text;
NSLog(@"%@ from date is",fromDates);
dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *strDate = [dtFormatter dateFromString:fromDates];
NSLog(@"First = %@",strDate);
}
Can anyone please tell me where im goin wrong.i completly stuck here.Thanks in advance. 谁能告诉我我哪里出错了。我完全呆在这里。在此先感谢。
Your date formatter doesn't follow the string format. 您的日期格式化程序不遵循字符串格式。
I think you're looking for this: 我认为您正在寻找这个:
[dtFormatter setDateFormat:@"dd/MM/yyyy"];
NSDate *date1 = [dtFormatter dateFromString:fromDates];
NSDate *date2 = [dtFormatter dateFromString:anotherDate];
if ([date1 compare:date2] == NSOrderedDescending)
NSLog(@"date1 comes first");
else if ([date1 compare:date2] == NSOrderedAscending)
NSLog(@"date2 comes first");
else
NSLog(@"date1 is the same date as date2");
dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:@"dd/MM/yyyy"];
NSDate *strDate = [[NSDate alloc] init];
strDate = [dtFormatter dateFromString:fromDates];
NSLog(@"First = %@",strDate);
I think it will be helpful to you. 我认为这将对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.