How to get current time based on TimeZone in object c?

How to get current time based on TimeZone in object c?

example-> Current Time for Asia / Calcutta.

+5
source share
1 answer

Try it.

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

and if you need a specific time zone date, see the code below.

 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); 
+3
source

Source: https://habr.com/ru/post/1272958/


All Articles