如何在 UIDatePicker 中指定时区
- (IBAction) buttonPressed {
NSDate *selected = [datePicker 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];
[alert release];
[message release];
所以我的问题是……如何在 Objective C 中使用 Datepicker 指定时区? 现在,如果我从 DatePicker 中选择一些值并按下显示警报的按钮 - 时间是错误的。这可能是因为时区不正确,但我不确定。嗯,任何想法都会很棒。在下图中,您可以看到我选择了 2:14 PM (14:14),但更改者说现在是 11:14,时区是 +000
更新 1 我对我的代码添加了一些更改,但仍然没有任何变化......
- (IBAction) buttonPressed {
NSDate *selected = [datePicker date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *tz = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:tz];
NSString *formattedDate = [dateFormatter stringFromDate:selected];
NSString *message = [[NSString alloc] initWithFormat:@"The date and time you selected is: %@", formattedDate];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Date and Time Selected"
message:message delegate:nil cancelButtonTitle:@"Yes, I did!" otherButtonTitles:nil];
[alert show];
[alert release];
[message release];
[dateFormatter release];
- (IBAction) buttonPressed {
NSDate *selected = [datePicker 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];
[alert release];
[message release];
So my question is next... How can I specify a time zone using a Datepicker in Objective C?
now if I choose some values from DatePicker and press the button which displays an alert - the time is wrong. It's probably because of incorrect time zone but I'm not sure. Well any ideas would be great. On the picture below you can see that I chose 2:14 PM (14:14) but an alter says that it is 11:14 and the time zone is +000
UPDATE 1
I have added a few changes to my code but still nothing...
- (IBAction) buttonPressed {
NSDate *selected = [datePicker date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *tz = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:tz];
NSString *formattedDate = [dateFormatter stringFromDate:selected];
NSString *message = [[NSString alloc] initWithFormat:@"The date and time you selected is: %@", formattedDate];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Date and Time Selected"
message:message delegate:nil cancelButtonTitle:@"Yes, I did!" otherButtonTitles:nil];
[alert show];
[alert release];
[message release];
[dateFormatter release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在定义日期选择器时使用它
datePicker.timeZone = [NSTimeZone localTimeZone];
Use this at the point where use defined your datepicker
datePicker.timeZone = [NSTimeZone localTimeZone];
您可以设置带有时区的日期
Objective C
在 Swift 3 中:
You can set the date with timezone
Objective C
in Swift 3:
在斯威夫特 3 中:
in Swift 3:
您可以使用时区字符串设置日期,例如
Australia/West (+08:00)、WST、Asia/Kolkata (+05:30)
、ISTYou can set the date with timezone strings like
Australia/West (+08:00) , WST, Asia/Kolkata (+05:30)
, IST我认为这是因为选择了日期格式,使用 NSDateFormatter 指定适当的日期格式并在字符串中使用它,这里是一个链接 链接
I think this is because of the date format being picked, use an NSDateFormatter to specify the appropriate date format and use it in your string, here is a link Link