xcode timePicker 返回错误的时间

标签 xcode datepicker datetimepicker

我的时间选择器出现问题,无法弄清楚我的问题是什么。这是我的代码;

timePicker.timeZone = [NSTimeZone localTimeZone];
NSDate *selected = [timePicker date];

NSString *message = [[NSString alloc] initWithFormat:
                     @"The date and time you selected is: %@", selected];
UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle:@"Date and Time Selected"
                      message:message
                      delegate:nil
                      cancelButtonTitle:@"Yes, I did."
                      otherButtonTitles:nil];
[alert show];

例如,如果我选择时间 2:55 pm,则警报将返回“2012-08-14 19:55:37 +0000”

它必须是非常简单的东西,只是看不到它

最佳答案

NSDate 没有时区的概念。它只存储中性时间信息。所以你必须纠正你的输出来补偿。

您可以创建 NSDateFormatter 的实例,然后 setTimeZone:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"..."];

//Timezone!
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];

NSString *dateSting = [dateFormatter stringFromDate:myDate];

关于xcode timePicker 返回错误的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11959944/

相关文章:

ios - 如何通过命令行创建目标

ios - 错误 - 线程 1 : signal SIGABRT (unrecognized selector sent to instance)

android - Android Lollipop 中的自定义日期选择器对话框

jquery - datetimepicker getDate 返回 UTC 格式的日期/时间

javascript - 是否有针对这个模糊的几分钟 iOS 日期时间本地错误的修复程序?

javascript - 如何在选择日期后关闭日期时间选择器?

ios - 更新在后台运行的标签的计时器

ios - UIImages 在 xcode 中丢失尺寸并变大

angular - DatePicker 更改自定义标题中的 View

javascript - Angular Material 的日期选择器的日期错误