2016-10-03 140 views
-1

dbVoiceDate不对,为什么?iOS NSDateFormatter返回错误日期

我试了很多!

NSDateFormatter *formatterVoice = [[NSDateFormatter alloc] init]; 
formatterVoice.dateFormat = @"dd-MMM-yyyy hh:mm:ss"; 
dbVoiceDate = [formatterVoice dateFromString:dbDateStr]; 

获得响应:

dbDateStr的印刷描述:

03-Oct-2016 10:43:59 

self->dbVoiceDate印刷描述:

2016-10-03 05:02:16 +0000 

在此先感谢。

+0

时区是问题,请参阅http://stackoverflow.com/questions/16621330/nsdateformatter-not-working-to-set-settimezone –

+0

http://stackoverflow.com/questions/8442706/nsdateformatter-and -time-zone-issue –

+0

什么样的约会你不会告诉我..然后我给你完美的答案。 –

回答

1

格式像这样

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"dd-MMM-yyyy hh:mm:ss"]; 
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; 

//then do your coding. 
0

您需要检查时区。如果是在格林尼治标准时间设置时区为GMT,像这样:

[youDateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; 

否则你可以使用本地时区是这样的:

[youDateFormat setTimeZone:[NSTimeZone systemTimeZone]]; 

希望这有助于... :)

1

尝试这一点: -

NSString *dateString = @"Thu Oct 3 10:34:58 +0000 2016"; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"EEE MMM dd hh:mm:ss Z yyyy"]; 
NSDate *date = [dateFormatter dateFromString:dateString]; 
NSLog(@"%@", date); 
[dateFormatter setDateFormat:@"dd-MM-yyyy hh:mm:ss"]; 
NSString *finalDateStr = [dateFormatter stringFromDate:date]; 
NSLog(@"%@", finalDateStr); 
1

试试这个下面的代码:

+(NSString*)getDateWithMonthSpelling:(NSString*)dateStr 
{ 
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; 
    dateFormatter.dateFormat = @"dd/MM/yyyy"; 
    NSDate *yourDate = [dateFormatter dateFromString:dateStr]; 
    dateFormatter.dateFormat = @"dd MMM yyyy"; 
    NSString*myDate = [dateFormatter stringFromDate:yourDate]; 
    return myDate; 
}