2014-12-19 83 views
-3

我的時間本地時間字符串是2014-12-20 16:30:00 GMT + 5:30,而且我需要將同一時間轉換爲不同的區域2014-12 -20 16:30:00 GMT + 2:30然後要轉換此同一時間2014-12-20 16:30:00 GMT + 2:3 0至UTC爲2014-12-20 14:00 :00作爲字符串。Xcode-不同時間轉換爲UTC

NSDateFormatter *df2 = [[NSDateFormatter alloc] init]; 
df2.dateStyle = NSDateFormatterMediumStyle; 
[df2 setDateFormat:@"YYYY-MM-dd HH:mm:ss z"]; 

[df2 setTimeZone:[NSTimeZone timeZoneWithName:timezonename]]; 
start_date=[df2 stringFromDate:datepick.date]; 

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss z'"]; 
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:timezonename]]; 
NSDate *dte = [dateFormat dateFromString:start_date]; 

NSLog(@"Date: %@--%@",start_date, dte); 

在此請幫助..

在此先感謝..

+0

我需要將本地時間轉換爲另一個時區而不更改時間2014-12-20 16:30:00 GMT + 5:30和2014-12-20 16:30:00 GMT + 2 :30然後轉到utc – Vinoth 2014-12-19 12:00:25

回答

0

使用timeZoneForSecondsFromGMTNSTimeZone並設置爲timezoneNSDateFormatter

NSString *dateString = @"2014-12-20 16:30:00 GMT+5:30"; 
NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; 

// For GMT + 2.30, seconds will be 7200 (2 hours) + 1800 (30 minutes) 
// For GMT + 5.30, seconds will be 18000 (5 hours) + 1800 (30 minutes) 
// 60 seconds * 60 minutes * 5 hours + 60 seconds * 30 minutes 
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:19800]; 
[formatter setTimeZone:timeZone]; 

NSDate *date =[formatter dateFromString:dateString]; 
NSString *newTimeZoneDateString = [formatter stringFromDate:date]; 
NSLog(@"%@",newTimeZoneDateString1); 
+0

我需要將本地時間轉換爲另一個時區(不同時區),而不更改時間爲2014-12-20 16:30:00 GMT + 5:30和2014-12-20 16:30:00 GMT + 2:30然後到utc。不僅需要+5.30 .. – Vinoth 2014-12-19 12:02:13

0
-(NSDate*)convertThisDate:(NSDate*)aDate 
      toThisTimeZone:(NSString*)timeZoneAbbreviation{ 


    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 

    NSDate *yourDate = [NSDate date]; 

    NSString *yourDateAsString = [dateFormatter stringFromDate:yourDate]; 

    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:timeZoneAbbreviation]]; 

    NSDate *convertedDate = [dateFormatter dateFromString:yourDateAsString]; 


    NSLog(@"convertedDate : %@",convertedDate); 

    return convertedDate; 

} 

//For Local Timezone 
-(NSDate*)convertThisDateToLocalTimeZone:(NSDate*)aDate{ 


    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 

    NSDate *yourDate = [NSDate date]; 

    NSString *yourDateAsString = [dateFormatter stringFromDate:yourDate]; 

    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];//current local time zone in device 

    NSDate *convertedDate = [dateFormatter dateFromString:yourDateAsString]; 


    NSLog(@"convertedDate : %@",convertedDate); 

    return convertedDate; 

} 

用法:

NSDate *UTCDate = [self convertThisDate:myDate 
          toThisTimeZone:@"UTC"]; 

    NSDate *GMTDate = [self convertThisDate:myDate 
          toThisTimeZone:@"GMT"]; 

    NSDate *ESTDate = [self convertThisDate:myDate 
          toThisTimeZone:@"EST"]; 

    //For Local Timezone 
    NSDate *localTimeZoneDate = [self convertThisDateToLocalTimeZone:myDate]; 

要獲得支持縮寫的列表,你可以做的NSLog時區列表,

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

TimeZone List: { 
    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"; 
} 
+0

我需要將本地時間轉換爲另一個時區(不同時區),而不必更改時間2014-12-20 16:30:00 GMT + 5:30和2014-12-20 16:30: 00 GMT + 2:30然後以utc .. – Vinoth 2014-12-19 11:59:45

+0

我們沒有改變時間,地點不同。所以只需要使用上述兩種方法即可。 – 2014-12-19 12:04:14

2

可以使用本守則。可能對你有幫助。

NSDate sourceDate = Yourdate; 

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:@"Australia/Melbourne"]; 

NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; // your device time . you can also change any time zone like above. 

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

NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset; 

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

以UTC來源發佈時間 – Vinoth 2014-12-19 12:09:57

+0

你想說什麼?請詳細說明,以便我能夠給你相關的答案。 – 2014-12-19 12:17:48

+0

我需要將本地時區轉換爲另一個時區(不同時區),而不會更改時間2014-12-20 16:30:00 GMT + 5:30和2014-12-20 16:30:00 GMT + 2:30然後以utc爲2014-12-20 14:00:00 +000 .. – Vinoth 2014-12-19 12:20:51