微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在UIDatePicker中指定时区

如何解决如何在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];
    

解决方法

        在使用定义您的日期选择器时使用 datePicker.timeZone = [NSTimeZone localTimeZone];     ,        您可以使用时区设置日期 目标C
datePicker.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:10*60*60]; // Like GMT+10
在Swift 3:
datePicker.timeZone = TimeZone(secondsFromGMT: 5*60*60)  // Like GMT+5
    ,        您可以使用zone4ѭ,IST等时区字符串设置日期
datePicker.timeZone = [NSTimeZone timeZoneWithName:@\"timezone string goes here\"] ;
    ,        在Swift 3:
datePicker.timeZone = NSTimeZone.local
    ,        我认为这是因为选择了日期格式,请使用NSDateFormatter指定适当的日期格式并在您的字符串中使用它,这是一个链接     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。