如何在目标c中根据TimeZone获取当前时间?

Nee*_*aro 5 iphone objective-c ios

如何在目标c中根据TimeZone获取当前时间?

示例 - >亚洲/加尔各答的当前时间.

Nir*_*cha 3

Try this..

    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);
Run Code Online (Sandbox Code Playgroud)

and if you want specific timezone date then see below code..

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);
Run Code Online (Sandbox Code Playgroud)