2013-05-27 71 views
0

有人能告诉我我的时间格式化器有什么问题吗?当我使用iPod 6.0的时间格式化工程。但是当我使用iphone 5 6.1时,formatter返回nil。NSDateFormatter在iphone 5中返回nil ios 6.1

这里是我关于timeFormatter的代码。

NSDateFormatter *tempFormatter = [[[NSDateFormatter alloc]init] autorelease]; 
    [tempFormatter setDateFormat:@"yyyy-MM-dd hh:mm"]; 
    NSString *dateandtime = @"2013-05-27 19:00"; 

    //set end date and time 
    NSDateFormatter *tempFormatterTo = [[[NSDateFormatter alloc]init] autorelease]; 
    [tempFormatterTo setDateFormat:@"yyyy-MM-dd hh:mm"]; 
    NSString *dateandtimeTo = @"2013-05-27 20:00"; 

    NSDate *startDate = [tempFormatter dateFromString:dateandtime]; 
    NSDate *endDate = [tempFormatterTo dateFromString:dateandtimeTo]; 

在此先感谢。请帮帮我。

+2

它应该像“yyyy-MM-dd HH:mm” –

+0

谢谢!!有用! – Edgar

回答

0

尝试使用此格式。用于24小时格式的HH

[tempFormatter setDateFormat:@"yyyy-MM-dd HH:mm"]; 
+0

谢谢!!有用! – Edgar

+0

欢迎亲爱的...如果你有你的答案,然后接受这个答案.... –

0

在这里您的格式问题,将其更改为yyyy-MM-dd HH:mm,当你想任何NSString日期转换为NSDate也用我的方法波纹管。这是我的自定义方法,只是贴上此方法在.m类..

-(NSDate *)convertStringToDate:(NSString *) date dateFormat:(NSString*)dateFormat{ 
     NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; 
     NSDate *nowDate = [[[NSDate alloc] init] autorelease]; 
     [formatter setDateFormat:dateFormat];// set format here which format in string date 
     date = [date stringByReplacingOccurrencesOfString:@"+0000" withString:@""]; 
     nowDate = [formatter dateFromString:date]; 
     // NSLog(@"date============================>>>>>>>>>>>>>>> : %@", nowDate); 
     return nowDate; 
} 

,并使用上述类似波纹管的方法...

NSDate *startDate = [self convertStringToDate:@"2013-05-27 19:00" dateFormate:@"yyyy-MM-dd HH:mm"]; 
NSDate *endDate = [self convertStringToDate:@"2013-05-27 20:00" dateFormate:@"yyyy-MM-dd HH:mm"]; 
0

试试这个

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm"]; 
NSDate* d = [df dateFromString:@"2013-05-27 19:00"]; 
NSLog(@"%@", d); 
相关问题