iOS 5 在 DatePicker 中选择日期

发布于 2024-12-09 16:50:39 字数 969 浏览 0 评论 0原文

我想在 DatePicker 中选择日期,但我遇到了 2 小时滞后的问题。

 // String From Date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *dateSelected = [datePicker date];
NSString *dateChoisie = [dateFormatter stringFromDate:dateSelected];
NSDate *dateFinal = [dateFormatter dateFromString:dateChoisie];

NSLog(@"dateSelected = %@", dateSelected);
NSLog(@"dateChoisie = %@", dateChoisie);
NSLog(@"dateFinal = %@", dateFinal);

结果 :

2011-10-15 00:16:11.129 QuickEvent[25459:707] dateSelected = 2011-10-14 22:20:05 +0000
2011-10-15 00:16:11.132 QuickEvent[25459:707] dateChoisie = 2011-10-15 00:20:05 +0200
2011-10-15 00:16:11.138 QuickEvent[25459:707] dateFinal = 2011-10-14 22:20:05 +0000

I want to have in my DatePicker selected date, but I have a problem with lag of 2 hours.

 // String From Date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *dateSelected = [datePicker date];
NSString *dateChoisie = [dateFormatter stringFromDate:dateSelected];
NSDate *dateFinal = [dateFormatter dateFromString:dateChoisie];

NSLog(@"dateSelected = %@", dateSelected);
NSLog(@"dateChoisie = %@", dateChoisie);
NSLog(@"dateFinal = %@", dateFinal);

Result :

2011-10-15 00:16:11.129 QuickEvent[25459:707] dateSelected = 2011-10-14 22:20:05 +0000
2011-10-15 00:16:11.132 QuickEvent[25459:707] dateChoisie = 2011-10-15 00:20:05 +0200
2011-10-15 00:16:11.138 QuickEvent[25459:707] dateFinal = 2011-10-14 22:20:05 +0000

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

夏有森光若流苏 2024-12-16 16:50:39

尝试使用这种格式。

NSTimeZone *sourceTimeZone =[NSTimeZone systemTimeZone];
NSTimeZone *destinationTimeZone = [NSTimeZone timeZoneForSecondsFromGMT:(TIME_ZONE_THAT_YOU_WANTED*3600)];

NSInteger sourceSeconds =[sourceTimeZone secondsFromGMTForDate:date];
NSInteger destinationSeconds =[destinationTimeZone secondsFromGMTForDate:date];
NSTimeInterval interval = destinationSeconds - sourceSeconds;

NSDate *convertedDate =[[[NSDate alloc]initWithTimeInterval:interval sinceDate:date] autorelease];
return convertedDate;

例如,您可以将 TIME_ZONE_THAT_YOU_WANTED = -8 应用于基于纽约时区的时间

谢谢,
阿伦.

Try using this kind of formatting..

NSTimeZone *sourceTimeZone =[NSTimeZone systemTimeZone];
NSTimeZone *destinationTimeZone = [NSTimeZone timeZoneForSecondsFromGMT:(TIME_ZONE_THAT_YOU_WANTED*3600)];

NSInteger sourceSeconds =[sourceTimeZone secondsFromGMTForDate:date];
NSInteger destinationSeconds =[destinationTimeZone secondsFromGMTForDate:date];
NSTimeInterval interval = destinationSeconds - sourceSeconds;

NSDate *convertedDate =[[[NSDate alloc]initWithTimeInterval:interval sinceDate:date] autorelease];
return convertedDate;

For an example you can apply TIME_ZONE_THAT_YOU_WANTED = -8 for New york timezone based time

Thanks,
Arun.

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