[英]UIDatePicker time
I have a UIDatePicker and a button. 我有一个UIDatePicker和一个按钮。 When I press the button I want to display in the log the message "Setting a reminder for x" where x is the time displayed by the date picker. 当我按下按钮时,我想在日志中显示消息“为x设置提醒”,其中x是日期选择器显示的时间。
Everytime I press the button, the time logged is precisely 3 hours behind the time displayed by the picker (the time displayed by the picker is my current time). 每次我按下按钮时,记录的时间恰好比选择器显示的时间晚3小时(选择器显示的时间就是我的当前时间)。 I suspect it has something to do with the time zone. 我怀疑这与时区有关。 I live in GMT +2 time zone (I guess it's +3 since we are in daylight saving time). 我居住在格林尼治标准时间+2时区(我想是+3,因为我们处于夏令时)。
Do you have any idea how I could make the time logged to be the same as the time displayed? 您是否知道如何使记录的时间与显示的时间相同?
Below is the method that gets executed when I press the button. 下面是当我按下按钮时执行的方法。 Thanks. 谢谢。
- (IBAction)addReminder:(id)sender {
[self.datePicker setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(@"Setting a reminder for %@", self.datePicker.date);
}
NSDate
is simple representation of some moment in time, it knows nothing about your local time. NSDate
是某个时间的简单表示,它对您的本地时间一无所知。 When you log it (actually, when description
) message is sent, it uses GMT to display itself. 当您发送日志(实际上是description
)时,它使用GMT进行显示。
UIDatePicker
uses your timezone by default (see https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDatePicker_Class/#//apple_ref/occ/instp/UIDatePicker/timeZone ). UIDatePicker
默认使用您的时区(请参阅https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDatePicker_Class/#//apple_ref/occ/instp/UIDatePicker/timeZone )。
So, here is how it works: 因此,这是它的工作方式:
NSDate
for it. DatePicker使用您的本地(GMT + 2)时区并为其创建NSDate
。 NSDate
and since it knows nothing about timezone, it simply displays itself as GMT. 您显示NSDate
并且由于它对时区一无所知,因此它只是将自身显示为GMT。 To display your date, your should use NSDateFormatter
. 要显示日期,您应该使用NSDateFormatter
。 It has Timezone property: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/#//apple_ref/occ/instp/NSDateFormatter/timeZone 它具有Timezone属性: https : //developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/#//apple_ref/occ/instp/NSDateFormatter/timeZone
You can also use NSCalendar
and its components. 您也可以使用NSCalendar
及其组件。
You should read this guide: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html since dates, formatters and calendars are not very clear in Cocoa unless you understand its concepts. 您应该阅读以下指南: https : //developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html,因为除非您了解Cocoa的概念,否则日期,格式器和日历并不十分清楚。
Try this... 尝试这个...
NSDate *now = [[NSDate alloc] init];
[self.datePicker setDate:now];
[self.picker setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:2*60*60]]; // GMT+2 Your timeZone
NSLog(@"Setting a reminder for %@", self.datePicker.date);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.