【问题标题】:convert locale timezone date time to LAS VEGAS timezone date time将语言环境时区日期时间转换为拉斯维加斯时区日期时间
【发布时间】:2015-05-27 05:50:45
【问题描述】:

我正在尝试使用时区格式将本地日期时间转换为拉斯维加斯日期时间。

NSDateFormatter *dateFormatterdatabase = [[NSDateFormatter alloc] init];

dateFormatterdatabase=[[NSLocale alloc]    initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatterdatabase setDateFormat:@"dd-MM-yy hh:mm a"];

NSDate *StarEvent = [[NSDate alloc] init];

StarEvent = [dateFormatterdatabase dateFromString:@"15-05-15 12:00 AM"];

如何将本地时区日期时间格式转换为拉斯维加斯时区格式日期时间。 ??

例如,这里的当前时间是 2015-05-26 11:21:00 IST 到 LAS VEGAS FORMAT 为 2015-05-26 22:52:36 PDT..

怎么做?

【问题讨论】:

  • 如何将本地日期时间/时区转换为拉斯维加斯日期/时间时区?
  • 有人知道答案吗?

标签: objective-c date datetime


【解决方案1】:
// *** Set your Destination TimeZone, for Las Vegas its 'PDT/PST, change TimeZone accordingly for other conversions ***'
NSTimeZone* destinationTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"PST"];
// *** Its your current system(device) time zone ***
NSTimeZone* sourceTimeZone = [NSTimeZone systemTimeZone];

// *** Set your date to convert ***
NSDate *currentDate = [NSDate date];

// *** Calc time difference ***
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:currentDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:currentDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

// *** set current real date ***
NSDate* date = [[NSDate alloc] initWithTimeInterval:interval sinceDate:currentDate];

// *** Set Date Formater ***
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:destinationTimeZone];
[df setDateStyle:NSDateFormatterShortStyle];
[df setTimeStyle:NSDateFormatterFullStyle];
NSLog(@"Converted Date %@",[df stringFromDate:date]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 2016-06-17
    • 2019-01-14
    • 1970-01-01
    相关资源
    最近更新 更多