2017-10-28 96 views
5

Jak uzyskać aktualny czas na podstawie Strefy czasowej w celu c?Jak uzyskać aktualny czas na podstawie Strefy czasowej w celu c?

przykład-> Aktualny czas dla Azji/Kalkuty.

+5

3 upvotes? Zszokowany ... istnieje wiele przykładów przez Internet ... –

+1

Możliwy duplikat [Konwertuj NSDate UTC na lokalny cel strefy czasowej-C] (https://stackoverflow.com/questions/1268509/convert-utc-nsdate-to-local -timezone-objective-c) – clemens

Odpowiedz

3

Spróbuj ..

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); 

a jeśli chcesz konkretną datę strefy czasowej następnie zobaczyć poniżej kodu ..

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 zaktualizował kod. proszę sprawdź to –