iOS - NSDate 不是 adding/subtracting 正确的时区差异

iOS - NSDate is not adding/subtracting Timezone difference right

我正在尝试将 UTC 格式的日期时间转换为不同的时区

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"PKT"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *finalDate = [dateFormatter dateFromString:@"2015-08-18T23:00:00"];

PKT 是 UTC+05:00 结果应该是 2015-08-19 04:00:00 +0000 而不是减法,结果是 2015-08-18 18:00:00 +0000

我做错了什么?

解决方案 1

    NSTimeInterval seconds; // assume this exists
    NSDate* ts_utc = [NSDate dateWithTimeIntervalSince1970:seconds];

    NSDateFormatter* df_utc = [[[NSDateFormatter alloc] init] autorelease];
    [df_utc setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    [df_utc setDateFormat:@"yyyy.MM.dd G 'at' HH:mm:ss zzz"];

    NSDateFormatter* df_local = [[[NSDateFormatter alloc] init] autorelease];
    [df_local setTimeZone:[NSTimeZone timeZoneWithName:@"EST"]];
    [df_local setDateFormat:@"yyyy.MM.dd G 'at' HH:mm:ss zzz"];

    NSString* ts_utc_string = [df_utc stringFromDate:ts_utc];
    NSString* ts_local_string = [df_local stringFromDate:ts_utc];
    // you can also use NSDateFormatter dateFromString to go the opposite way

解决方案 2

-(NSString *)getUTCFormateDate:(NSDate *)localDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    [dateFormatter setTimeZone:timeZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:localDate];
    [dateFormatter release];
    return dateString;
}

这里是缩写词典

NSLog(@"timezones: %@", [NSTimeZone abbreviationDictionary]);
timezones: {
    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";
}

我仍然不知道是什么导致了这个错误的 add/sub 时间。如果我通过 UTC+05:00 作为时区缩写,它会减去,如果我通过 UTC-05:00 我会加起来时间。我找到了一个解决方法并解决了这个问题这里是代码

+(NSString*)getDBDate_UTC:(NSString*)date{

NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
[formatter1 setCalendar:[NSCalendar calendarWithIdentifier:NSCalendarIdentifierISO8601]];
[formatter1 setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];
[formatter1 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSDate* sourceDate = [formatter1 dateFromString:date];

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC+05:00"];
NSTimeZone* destinationTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];

NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];

return [formatter1 stringFromDate:destinationDate];

}

希望对您有所帮助