将[NSDATE DATE]转换为本地化的UTC日期字符串



我有一个我使用的 NSDate对象,该对象是我们使用的API。我们的应用程序在世界各地都使用,因此API希望日期在UTC中,例如:

"/Date(1481704200000)/"

但是,由于API/服务器没有为我们这样做,因此需要考虑到时区偏移量。如果我们不这样做,则在服务器上查看它时的日期是不正确的。

我正在做以下操作,这似乎会产生不正确的日期:

// Offset
float timezoneoffset = [[NSTimeZone localTimeZone] secondsFromGMT];
// Date
CFTimeInterval startDate = [[NSDate date] timeIntervalSince1970];
startDate = startDate - timezoneoffset;
NSString *dateStarted = [NSString stringWithFormat:@"/Date(%.0f000)/", startDate];

如何正确考虑本地时区并创建正确格式的UTC日期字符串?

无需任何时区操作。

NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
NSString *dateStarted = [NSString stringWithFormat:@"/Date(%.0f)/", interval * 1000];

这为您提供UTC的时间,无论用户的时区如何。

尝试。在测试设备上使用几个不同时区设置运行此代码,在每种情况下您都会获得相同的结果。

NSDate *currentDate = [NSDate date];

现在是在UTC中(至少在使用以下方法之后)将这一次作为UTC存储(自1970年以来)使用

double secsUtc1970 = [[NSDate date]timeIntervalSince1970];

将日期格式化设置为输出本地时间:

NSTimeZone *timeZone = [NSTimeZone defaultTimeZone];
// or Timezone with specific name like
// [NSTimeZone timeZoneWithName:@"Europe/Riga"] 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSString *localDateString = [dateFormatter stringFromDate:currentDate];

NSDate对象始终使用UTC作为时间参考,但是日期的字符串表示并非基于UTC时区。

要获取时区,您可以使用以下方式:

NSTimeZone* timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSString* timeZoneName = [timeZone localizedName:NSTimeZoneNameStyleStandard locale:[NSLocale currentLocale]];
NSLog(@"Name : %@", timeZoneName);

如果您致电[NSTimeZone abbreviationDictionary],您将获得一个包含所有可用缩写的字典。

NSLog(@"%@",[NSTimeZone abbreviationDictionary]);

结果:

2015-07-10 11:44:05.954 APP[1472:26120] {
    ADT = "America/Halifax";
    AKDT = "America/Juneau";
    AKST = "America/Juneau";
    ART = "America/Argentina/Buenos_Aires";
    AST = "America/Halifax";
    BDT = "Asia/Dhaka";
    BRST = "America/Sao_Paulo";
    BRT = "America/Sao_Paulo";
    BST = "Europe/London";
    CAT = "Africa/Harare";
    CDT = "America/Chicago";
    CEST = "Europe/Paris";
    CET = "Europe/Paris";
    CLST = "America/Santiago";
    CLT = "America/Santiago";
    COT = "America/Bogota";
    CST = "America/Chicago";
    EAT = "Africa/Addis_Ababa";
    EDT = "America/New_York";
    EEST = "Europe/Istanbul";
    EET = "Europe/Istanbul";
    EST = "America/New_York";
    GMT = GMT;
    GST = "Asia/Dubai";
    HKT = "Asia/Hong_Kong";
    HST = "Pacific/Honolulu";
    ICT = "Asia/Bangkok";
    IRST = "Asia/Tehran";
    IST = "Asia/Calcutta";
    JST = "Asia/Tokyo";
    KST = "Asia/Seoul";
    MDT = "America/Denver";
    MSD = "Europe/Moscow";
    MSK = "Europe/Moscow";
    MST = "America/Denver";
    NZDT = "Pacific/Auckland";
    NZST = "Pacific/Auckland";
    PDT = "America/Los_Angeles";
    PET = "America/Lima";
    PHT = "Asia/Manila";
    PKT = "Asia/Karachi";
    PST = "America/Los_Angeles";
    SGT = "Asia/Singapore";
    UTC = UTC;
    WAT = "Africa/Lagos";
    WEST = "Europe/Lisbon";
    WET = "Europe/Lisbon";
    WIT = "Asia/Jakarta";
}

希望这会有所帮助。

public let Date_format_Default:String = "yyyy-MM-dd HH:mm:ss"

为UTC字符串设置此格式字符串;

public let Date_format_Z:String = "yyyy-MM-dd HH:mm:ssZ"
public let Time_zone_UTC:String = "UTC"
open class func convertDate2UTC(_ date:String)->(String){
    var df_:DateFormatter?=DateFormatter()
    df_!.dateFormat=Date_format_Z
    let tz_=TimeZone(identifier: Time_zone_UTC)
    df_!.timeZone=tz_
    let df_s=df_!.date(from: date)
    df_!.dateFormat=Date_format_Default
    let string_=df_!.string(from: df_s!)
    df_ = nil
    return string_;
}