How to get the current time based on TimeZone in objective c?
example-> Current time for Asia/Kolkata.
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With