2013-05-27 150 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); 
相關問題