2017-10-28 14 views
5

उद्देश्य सी में टाइमज़ोन के आधार पर वर्तमान समय कैसे प्राप्त करें?उद्देश्य सी में टाइमज़ोन के आधार पर वर्तमान समय कैसे प्राप्त करें?

उदाहरण-> एशिया/कोलकाता के लिए वर्तमान समय।

+5

3 upvotes? चौंक गया ... इंटरनेट पर कई नमूने हैं ... –

+1

संभावित यूपीटी एनएसडीएटी को स्थानीय टाइमज़ोन उद्देश्य-सी में कनवर्ट करें (https://stackoverflow.com/questions/1268509/convert-utc-nsdate-to-local -टाइज़ोन-उद्देश्य-सी) – clemens

उत्तर

3

इस प्रयास करें ..

NSDateFormatter *dateFormatter = [NSDateFormatter new]; 
    [dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm"]; 

    //Create the date assuming the given string is in GMT 
    dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]; 

    //Create a date string in the local timezone 
    dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone localTimeZone].secondsFromGMT]; 
    NSString *localDateString = [dateFormatter stringFromDate:[NSDate date]]; 
    NSLog(@"date = %@", localDateString); 

और यदि आप विशिष्ट क्षेत्रों के बीच व्याप्त तारीख चाहते हैं तो नीचे दिए गए कोड को देखने के ..

NSDate *myDate = [NSDate date]; 
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; 
    [dateFormatter1 setDateStyle:NSDateFormatterLongStyle]; 
    [dateFormatter1 setTimeStyle:NSDateFormatterLongStyle]; 
    [dateFormatter1 setDateFormat:@"dd/MM/yyy hh:mm a"]; 

NSTimeZone *timeZone = [NSTimeZone timeZoneWithName: @"Asia/Calcutta"]; 
[dateFormatter1 setTimeZone:timeZone]; 

NSString *dateString = [dateFormatter1 stringFromDate: myDate]; 
NSLog(@"%@", dateString); 
+0

@NeerajSonaro कोड अपडेट किया गया। कृपया यह जाँचें –

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