2012-07-26 12 views
10

इनपुट स्ट्रिंग: 14 जून 2012 - 01:00:00 यूटीसीकैसे स्थानीय समय में यूटीसी दिनांक स्ट्रिंग कन्वर्ट करने के लिए (systemTimeZone)

आउटपुट स्थानीय स्ट्रिंग: जून 13, 2012 - 21:00:00 EDT

मैं से

NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; 
NSLog(@"Time Zone: %@", destinationTimeZone.abbreviation); 

कोई भी सुझाव ऑफसेट करने के लिए पसंद है?

उत्तर

23

से लिया के लिए एक सा यह करना चाहिए संशोधित कर सकते हैं कि तुम क्या जरूरत है:

NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; 
fmt.dateFormat = @"LLL d, yyyy - HH:mm:ss zzz"; 
NSDate *utc = [fmt dateFromString:@"June 14, 2012 - 01:00:00 UTC"]; 
fmt.timeZone = [NSTimeZone systemTimeZone]; 
NSString *local = [fmt stringFromDate:utc]; 
NSLog(@"%@", local); 

ध्यान दें कि आपका उदाहरण गलत है: जब यूटीसी में जून -14 वें स्थान पर 1 बजे है, तो यह अभी भी ईएसटी में जून -13 है, 8 बजे सेंट एंडर्ड या 9 बजे डेलाइट बचत समय। अपने सिस्टम पर इस कार्यक्रम के प्रिंट

Jun 13, 2012 - 21:00:00 EDT 
2

स्थानीय समय में GMT से यह परिवर्तित, आप इसे यूटीसी समय

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm"; 

NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
[dateFormatter setTimeZone:gmt]; 
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]]; 
[dateFormatter release]; 

iPhone: NSDate convert GMT to local time

4
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter.dateFormat = @"MMMM d, yyyy - HH:mm:ss zzz"; // format might need to be modified 

NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; 
[dateFormatter setTimeZone:destinationTimeZone]; 

NSDate *oldTime = [dateFormatter dateFromString:utcDateString]; 

NSString *estDateString = [dateFormatter stringFromDate:oldTime]; 
2

स्विफ्ट 3

var dateformat = DateFormatter() 
dateformat.dateFormat = "LLL d, yyyy - HH:mm:ss zzz" 
var utc: Date? = dateformat.date(fromString: "June 14, 2012 - 01:00:00 UTC") 
dateformat.timeZone = TimeZone.current 
var local: String = dateformat.string(from: utc) 
print(local) 


स्विफ्ट 4: दिनांक एक्सटेंशन यूटीसी या जीएमटी ⟺ स्थानीय

//UTC or GMT ⟺ Local 

extension Date { 

    // Convert local time to UTC (or GMT) 
    func toGlobalTime() -> Date { 
     let timezone = TimeZone.current 
     let seconds = -TimeInterval(timezone.secondsFromGMT(for: self)) 
     return Date(timeInterval: seconds, since: self) 
    } 

    // Convert UTC (or GMT) to local time 
    func toLocalTime() -> Date { 
     let timezone = TimeZone.current 
     let seconds = TimeInterval(timezone.secondsFromGMT(for: self)) 
     return Date(timeInterval: seconds, since: self) 
    } 

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