NSDate格式问题 [英] NSDate format issue
本文介绍了NSDate格式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是来自nsdate格式化程序的代码...由于某种原因,值dateSelected不正确...而不是2011年4月30日下午7:55它返回2011-05-01 02:55 ...任何想法我做错了什么?
NSDateFormatter * outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@h:mm a];
objEventInsert.eventtime = [outputFormatter stringFromDate:self.datePicker.date];
NSLog(@%@,objEventInsert.eventtime);
NSDateFormatter * dateForm = [[NSDateFormatter alloc] init];
[dateForm setDateFormat:@LLLL d y h:mm a];
NSDate * dateSelected = [dateForm dateFromString:[NSString stringWithFormat:@%@%@,objEventInsert.eventstartdate,objEventInsert.eventtime]];
NSLog(@%@,objEventInsert.eventstartdate);
objEventInsert.date = dateSelected;
NSLog(@%@,objEventInsert.date);
NSLog响应.. 。
2011-04-30 19:54:14.264 APP [24017:207] 7:55 PM
2011 -04-30 19:54:16.216 APP [24017:207] 2011年4月30日
2011-04-30 19:54:17.654 APP [24017:207] 2011-05-01 02:55:00 +0000
解决方案
这是正确的UTC时间。您需要设置区域设置/时区以获取当地时间,即7:55。
请参阅这些答案示例
- 使用NSDateFormatter 不一致的行为在两个不同的设备上
- NSDate dateFromString,如何解析'围绕'UTC,GMT和用户区域设置?
Here is the code from the nsdate formatter... for some reason the value dateSelected is incorrect... instead of "April 30 2011 7:55PM" it returns 2011-05-01 02:55... any idea what am i doing wrong?
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"h:mm a"];
objEventInsert.eventtime = [outputFormatter stringFromDate:self.datePicker.date];
NSLog(@"%@",objEventInsert.eventtime);
NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];
[dateForm setDateFormat:@"LLLL d y h:mm a"];
NSDate *dateSelected = [dateForm dateFromString:[NSString stringWithFormat:@"%@ %@",objEventInsert.eventstartdate,objEventInsert.eventtime]];
NSLog(@"%@",objEventInsert.eventstartdate);
objEventInsert.date = dateSelected;
NSLog(@"%@",objEventInsert.date);
NSLog response...
2011-04-30 19:54:14.264 APP[24017:207] 7:55 PM
2011-04-30 19:54:16.216 APP[24017:207] April 30 2011
2011-04-30 19:54:17.654 APP[24017:207] 2011-05-01 02:55:00 +0000
解决方案
That's the correct UTC time. You'll need to set the locale/timezone to get the local time, i.e. 7:55.
See these answer examples
- Inconsistent behaviour with NSDateFormatter on two different devices
- NSDate dateFromString, how to parse 'around' UTC, GMT and User locale?
这篇关于NSDate格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文