Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current time based on TimeZone in objective c?

How to get the current time based on TimeZone in objective c?

example-> Current time for Asia/Kolkata.

like image 223
Neeraj Sonaro Avatar asked Oct 28 '17 08:10

Neeraj Sonaro


1 Answers

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);
like image 84
iNiravKotecha Avatar answered Nov 03 '22 00:11

iNiravKotecha