gpt4 book ai didi

iphone - 如何在 UIDatePicker 中指定时区

转载 作者:太空狗 更新时间:2023-10-30 03:27:37 27 4
gpt4 key购买 nike

- (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(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];

最佳答案

在定义日期选择器的地方使用它

datePicker.timeZone = [NSTimeZone localTimeZone];

关于iphone - 如何在 UIDatePicker 中指定时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6395629/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com