UIDatePicker 给出错误的时间 [英] UIDatePicker giving wrong time
问题描述
可能的重复:
将日期从 [NSDate 日期] 减少几个小时
NSDate 日期不给我正确的时间 >
我有这段代码:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];[格式化程序 setDateFormat:@"HH:mm"];NSString *formattedDateInString = [formatter stringFromDate:[self.datePicker date]];NSLog(@"这是格式化日期(STRING) ---> %@" ,formattedDateInString);self.fromHourLabel.text = formattedDateInString;NSDate *date = [格式化日期FromString:formattedDateInString];NSLog(@"这是日期 (NSDate) ---> %@" ,date);self.fromHour = 日期;
我基本上从 UIDatePicker 获取日期,将小时和分钟设置为标签文本,并将日期保存到名为 fromHour 的属性中.在 -viewDidLoad
方法中,我也有:
self.datePicker.timeZone = [NSTimeZone localTimeZone];
这是输出:
如您所见,使用此代码,我有两个主要问题:
- 第一个输出是 17.30,第二个是 16:30(第一个输出是正确的,第二个是错误的)为什么?
- 第二个输出的格式为yyyy-MM-dd HH:mm:ss",但我只需要HH:mm".
你能帮我吗?
我知道有几十个现有问题涵盖了这一点,但我找不到一个.
问题很简单.当您使用 NSLog
或包含 %@
格式说明符的字符串格式打印对象时,将在对象上调用 description
方法.>
NSDate
的 description
方法总是使用祖鲁时区 (+0000) 显示完整日期.
如果您想以特定格式打印日期,请使用 NSDateFormatter
转换为字符串并打印该字符串.
根据评论中的信息,这里的部分问题是您从正确的 NSDate
开始.然后将其转换为 HH:mm
格式的字符串.然后从该字符串创建一个新的 NSDate
.更好的解决方案是简单地保存原始日期并避免创建第二个日期.
Possible Duplicate:
Getting date from [NSDate date] off by a few hours
NSDate date don’t give me the right hour
I have this piece of code:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm"];
NSString *formattedDateInString = [formatter stringFromDate:[self.datePicker date]];
NSLog(@"This is the formatted date (STRING) ---> %@" ,formattedDateInString);
self.fromHourLabel.text = formattedDateInString;
NSDate *date = [formatter dateFromString:formattedDateInString];
NSLog(@"This is the date (NSDate) ---> %@" ,date);
self.fromHour = date;
I basically get the date from a UIDatePicker, set Hours and Minutes as a label text and save the date into a property called fromHour. In the -viewDidLoad
method I also have:
self.datePicker.timeZone = [NSTimeZone localTimeZone];
This is the output:
as you can see, with this code, I have two main problems:
- The first output say 17.30 and the second 16:30 (the first output is correct, the second is wrong) why?
- The second output is formatted with "yyyy-MM-dd HH:mm:ss" but I need only "HH:mm".
Can you help me?
I know there are dozens of existing questions that cover this but I can't find one.
The problem is simple. When you print an object using NSLog
or a string format containing the %@
format specifier, the description
method is called on the object.
The description
method of NSDate
always displays the full date using the Zulu timezone (+0000).
If you want a date printed in a specific format then use NSDateFormatter
to convert to a string and print the string.
Edit: Based on info from the comments, part of the problem here is that you start with the proper NSDate
. You then convert that to a string with the format HH:mm
. You then create a new NSDate
from that string. The better solution is to simply save the original date and avoid the creation of the 2nd date.
这篇关于UIDatePicker 给出错误的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!