UIDatePicker 给出错误的时间

编程入门 行业动态 更新时间:2024-10-23 11:23:25
本文介绍了UIDatePicker 给出错误的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能的重复:将日期从 [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 给出错误的时间

本文发布于:2023-10-28 01:17:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535076.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   时间   UIDatePicker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!