2013-08-03 7 views
5

UTC समयज़ोन परिवर्तित मैं करने के लिए डिवाइस स्थानीय समय क्षेत्र निम्नलिखित समय क्षेत्र परिवर्तित करने के लिए कोशिश कर रहा हूँ:iOS: करने के लिए डिवाइस स्थानीय समय क्षेत्र

2013-08-03T05:38:39.590Z 

कृपया मुझे पता है कि कैसे स्थानीय समय क्षेत्र के लिए यह कन्वर्ट करने के लिए करते हैं।

मैं इसे कैसे कर सकता हूं?

+0

की [कैसे iPhone डिवाइस के समय क्षेत्र के समय परिवर्तित करने के लिए?] (Http संभव डुप्लिकेट://stackoverflow.com/questions/1081647/how-to-convert-time-to-the-timezone-of-the-iphone-device) –

उत्तर

11

समय क्षेत्र NSDateFormatter पर लागू किया जा सकता है, जिससे आप समय अंतर से अलग एनएसडीएट चर उत्पन्न कर सकते हैं।

NSString* input = @"2013-08-03T05:38:39.590Z"; 
NSString* format = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; 

// Set up an NSDateFormatter for UTC time zone 
NSDateFormatter* formatterUtc = [[NSDateFormatter alloc] init]; 
[formatterUtc setDateFormat:format]; 
[formatterUtc setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; 

// Cast the input string to NSDate 
NSDate* utcDate = [formatterUtc dateFromString:input]; 

// Set up an NSDateFormatter for the device's local time zone 
NSDateFormatter* formatterLocal = [[NSDateFormatter alloc] init]; 
[formatterLocal setDateFormat:format]; 
[formatterLocal setTimeZone:[NSTimeZone localTimeZone]]; 

// Create local NSDate with time zone difference 
NSDate* localDate = [formatterUtc dateFromString:[formatterLocal stringFromDate:utcDate]]; 

NSLog(@"utc: %@", utcDate); 
NSLog(@"local: %@", localDate); 

[formatterUtc release]; 
[formatterLocal release]; 
1

इस तरह का प्रयास करें:

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"]; 
    NSDate* utcTime = [dateFormatter dateFromString:@"2013-08-03T05:38:39.590Z"]; 
    NSLog(@"UTC time: %@", utcTime); 

    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; 
    [dateFormatter setDateFormat:@"M/d/yy 'at' h:mma"]; 
    NSString* localTime = [dateFormatter stringFromDate:utcTime]; 
    NSLog(@"localTime:%@", localTime); 

आशा है कि यह आप में मदद करता है ....

+0

येसएसएस ने किया। बहुत बहुत दोस्त धन्यवाद –

संबंधित मुद्दे