2012-04-02 81 views
0
NSDate *date=[[NSDate alloc] init]; 
    NSDateFormatter *dfLocal=[[NSDateFormatter alloc]init]; 
    [dfLocal setDateFormat:@"yyyy-MM-dd"]; 
    date=[dfLocal dateFromString:@"2011-09-22"]; 
    NSLog(@"the date is %@",date); 

這是我的代碼,並給出了輸出做錯了..什麼我在此日期格式

2012-04-02 18:13:45.338 TimeProblem[2656:207] the date is 2011-09-21 18:30:00 +0000 

爲什麼會追加這個.. 18:30:00 +0000到底..?我只是想它是00:00:00 +0000

回答

3

用途:

NSDate *date=[[NSDate alloc] init]; 
NSDateFormatter *dfLocal=[[NSDateFormatter alloc]init]; 
[dfLocal setDateFormat:@"yyyy-MM-dd"]; 
[dfLocal setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; 
date=[dfLocal dateFromString:@"2011-09-22"]; 

NSLog(@"the date is %@",date); 
0

嗨,你想獲得日期和時間,然後像這樣使用。

[dfLocal setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 

希望這會幫助你。下面

+0

如果我使用上面的代碼,則返回null。 – 2012-04-02 12:58:09

1

您在GMT + 5.30。這就是爲什麼你得到時間抵消。這是一個時區問題。添加這條線可以解決問題。

[dfLocal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];