2017-10-28 21 views
5

Làm thế nào để có được thời gian hiện tại dựa trên TimeZone trong mục tiêu c?Làm thế nào để có được thời gian hiện tại dựa trên TimeZone trong mục tiêu c?

ví dụ-> Thời gian hiện tại cho châu Á/Kolkata.

+5

3 upvotes? Sốc ... có nhiều mẫu trên internet ... –

+1

Bản sao có thể có của [Chuyển đổi UTC NSDate thành Giờ địa phương mục tiêu-C] (https://stackoverflow.com/questions/1268509/convert-utc-nsdate-to-local -timezone-objective-c) – clemens

Trả lời

3

Hãy thử điều này ..

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

và nếu bạn muốn cập nhật múi giờ cụ thể sau đó xem dưới mã ..

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 đã cập nhật mã. xin vui lòng kiểm tra xem nó –

Các vấn đề liên quan