NSDateFormatter 给出不同的输出/错误(GMT?)时间 [英] NSDateFormatter gives different output/wrong (GMT?) time

查看:64
本文介绍了NSDateFormatter 给出不同的输出/错误(GMT?)时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尝试设置 NSDateFormatter 的时区和语言环境,但我似乎无法进行任何操作.这是代码

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];[dateFormat setDateFormat:@"MM/dd/yyyy hh:mm:ss"];NSString *myFireDateString=[@"9/17/11" stringByAppendingString:@" 09:00:00"];NSDate *myFireDate = [dateFormat dateFromString:myFireDateString];NSLog(@"日期字符串是 %@",myFireDateString);NSLog(@"格式化日期为%@",myFireDate);

输出如下:

日期字符串是 9/17/11 09:00:00

格式化日期为2011-09-17 13:00:00

解决方案

您需要为 dateFormatter 设置时区:

[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone];

参见 NSDate 来自 NSString ,显示前一天

I have tried setting the timezone and locale of the NSDateFormatter but I can't seem to get anything to work. Here is the code

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy hh:mm:ss"];
NSString *myFireDateString=[@"9/17/11" stringByAppendingString:@" 09:00:00"];
NSDate *myFireDate = [dateFormat dateFromString:myFireDateString];  
NSLog(@"The datestring is is %@",myFireDateString);
NSLog(@"The formatted date is %@",myFireDate);

Here is the output:

The datestring is is 9/17/11 09:00:00

The formatted date is 2011-09-17 13:00:00

解决方案

You need to set a timezone to your dateFormatter:

[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone];

See NSDate from NSString , shows the previous day

这篇关于NSDateFormatter 给出不同的输出/错误(GMT?)时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆