如何在 UIDatePicker 中指定时区

发布于 2024-11-15 23:02:25 字数 1394 浏览 0 评论 0原文

- (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

国产ˉ祖宗 2024-11-22 23:02:25

在定义日期选择器时使用它

datePicker.timeZone = [NSTimeZone localTimeZone];

Use this at the point where use defined your datepicker

datePicker.timeZone = [NSTimeZone localTimeZone];

难如初 2024-11-22 23:02:25

您可以设置带有时区的日期

Objective C

datePicker.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:10*60*60]; // Like GMT+10

在 Swift 3 中:

datePicker.timeZone = TimeZone(secondsFromGMT: 5*60*60)  // Like GMT+5

You can set the date with timezone

Objective C

datePicker.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:10*60*60]; // Like GMT+10

in Swift 3:

datePicker.timeZone = TimeZone(secondsFromGMT: 5*60*60)  // Like GMT+5
荒路情人 2024-11-22 23:02:25

在斯威夫特 3 中:

datePicker.timeZone = NSTimeZone.local

in Swift 3:

datePicker.timeZone = NSTimeZone.local
清晨说晚安 2024-11-22 23:02:25

您可以使用时区字符串设置日期,例如 Australia/West (+08:00)、WST、Asia/Kolkata (+05:30)、IST

datePicker.timeZone = [NSTimeZone timeZoneWithName:@"timezone string goes here"] ;

You can set the date with timezone strings like Australia/West (+08:00) , WST, Asia/Kolkata (+05:30) , IST

datePicker.timeZone = [NSTimeZone timeZoneWithName:@"timezone string goes here"] ;
因为看清所以看轻 2024-11-22 23:02:25

我认为这是因为选择了日期格式,使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文