iOS: convert UTC timezone to device local timezone - ios

I am trying to convert the following timezone to device local timezone:
2013-08-03T05:38:39.590Z
Please let me know how to convert it to local timezone.
How do I do it?

Time zones can be applied to NSDateFormatter, from which you can generate NSDate variables differentiated by the time difference.
NSString* input = #"2013-08-03T05:38:39.590Z";
NSString* format = #"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
// Set up an NSDateFormatter for UTC time zone
NSDateFormatter* formatterUtc = [[NSDateFormatter alloc] init];
[formatterUtc setDateFormat:format];
[formatterUtc setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// Cast the input string to NSDate
NSDate* utcDate = [formatterUtc dateFromString:input];
// Set up an NSDateFormatter for the device's local time zone
NSDateFormatter* formatterLocal = [[NSDateFormatter alloc] init];
[formatterLocal setDateFormat:format];
[formatterLocal setTimeZone:[NSTimeZone localTimeZone]];
// Create local NSDate with time zone difference
NSDate* localDate = [formatterUtc dateFromString:[formatterLocal stringFromDate:utcDate]];
NSLog(#"utc: %#", utcDate);
NSLog(#"local: %#", localDate);
[formatterUtc release];
[formatterLocal release];

Try like this:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate* utcTime = [dateFormatter dateFromString:#"2013-08-03T05:38:39.590Z"];
NSLog(#"UTC time: %#", utcTime);
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"M/d/yy 'at' h:mma"];
NSString* localTime = [dateFormatter stringFromDate:utcTime];
NSLog(#"localTime:%#", localTime);
Hope it helps you....

Related

How to convert UTC date string to local time

I currently get time as UTC and I want it to be convert into local time zone
NSDate * startDate;
Example strDate - 14/9/2017 7.28 Am
Required format - 14/9/2017 1.52 pm
I need help with objective c.
NSDate * startDate ; //contain utc date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, MMM d, yyyy - h:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:startDate];
but this is not correct time but date is correct
First convert NSString to NSDate.
NSDateFormatter *DateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZoneEDT =[NSTimeZone timeZoneWithName:#"GMT"];
[DateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[DateFormatter setTimeZone:timeZoneEDT];
[DateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS"];
NSDate *CurrentDate = [DateFormatter dateFromString:DateStr];
Then convert GMT time to local time.
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:#"Asia/Kolkata"];
[dateFormatter setTimeZone:sourceTimeZone];
NSString *dateRepresentation = [dateFormatter stringFromDate:dateFromString];
#athira try this. it works for me.
NSDate *startDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/M/yyyy h.mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:startDate];
NSLog(#"date = %#",timestamp); //date = 14/9/2017 4.34 PM
it will print current time with proper GMT+5:30 and i have used localTimeZone.

How to get the UTC time from NSDate iOS

I want to log the server time in my iOS app , I am using NHNetworkTime library to get the network time, when I try to print the following code its showing GMT, through I have chosen UTC, how to print the date time in UTC?
NSDate* datetime = [NSDate networkDate];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]]; // Prevent adjustment to user's local time zone.
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS Z z "];
NSString* dateTimeInIsoFormatForZuluTimeZone = [dateFormatter stringFromDate:datetime];
NSLog(#"%#",dateTimeInIsoFormatForZuluTimeZone);
this is printing 2016-03-09 06:51:23.406 +0000 GMT
There is no time difference between Coordinated Universal Time (UTC) and Greenwich Mean Time(GMT). you can get more info from here More info
Refer :-
-(NSString *)getUTCFormateDate:(NSDate *)localDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:localDate];
[dateFormatter release];
return dateString;
}
Moreover to get time
-(NSString *)getTimeFromDate:(NSDate *)localDate
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init]autorelease];
dateFormatter.dateFormat = #"MM/dd/yy";
NSString *dateString = [dateFormatter stringFromDate: localDate];
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc]init]autorelease];
timeFormatter.dateFormat = #"HH:mm:ss";
return [timeFormatter stringFromDate: localDate];
}

NSDateFormatter stringFromDate: returning local time instead of UTC / GMT

inside my iOS App I have an NSDate Object that should be converted to a NSString showing date and time in UTC / GMT:
NSDateFormatter * dateTimeFormatter = [[NSDateFormatter alloc] init];
[dateTimeFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
// OR [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
// OR [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
// OR [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
// OR [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// OR not specify the TimeZone at all...
NSString *result = [dateTimeFormatter stringFromDate:date];
When I set a breakpoint and examine date it is correctly displayed as __NSDate * 2015-08-25 14:03:57 UTC. However the resulting string is 2015-08-25 16:03:57, which is the same date in my local TimeZone (CEST).
No matter which solution I tried to get a string in UTC /GMT format 2015-08-25 14:03:57, I always get the local version 2015-08-25 16:03:57.
All sources I found state, that this should work. Where is my mistake?
This code should works:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[formatter setTimeZone:timeZone];
NSDate *now = [NSDate date];
NSString *dateAsString = [formatter stringFromDate:now];
The only strange thing I see in your code it's that you use dateFormatter instead of dateTimeFormatter when set the NSTimeZone.

How can i get local timezone in ios? [duplicate]

This question already has answers here:
Get current iPhone device timezone date and time from UTC-5 timezone date and time iPhone app?
(3 answers)
Closed 8 years ago.
I need to set local timezones as per the location of the ios device.
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
or
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"GTC+5.30"]];
is suited?
// get current date/time
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *currentTime = [dateFormatter stringFromDate:today];
[dateFormatter release];
NSLog(#"User's current time in their preference format:%#",currentTime);
You can get the name of the local time zone using:
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSString *tzName = [timeZone name];
This is how I do it:
NSString *strDateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSS";
// ---------------------------------------------------------
// input date is UTC date time, need to convert that time
// to user's own local device timezone to get correct date
// ---------------------------------------------------------
NSDateFormatter *dateFormatterUTC = [[NSDateFormatter alloc] init];
[dateFormatterUTC setDateFormat:strDateFormat];
[dateFormatterUTC setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// get UTC Date first
NSDate* utcDate = [dateFormatterUTC dateFromString:strDate];
// get local date from UTC Date
NSDateFormatter *dateFormatterLocal = [[NSDateFormatter alloc] init];
[dateFormatterLocal setDateFormat:strDateFormat];
[dateFormatterLocal setTimeZone:[NSTimeZone systemTimeZone]];
// get final local date calculated by offsetting UTC Date
NSDate *localDate = [dateFormatterUTC dateFromString:[dateFormatterLocal stringFromDate:utcDate]];
NSDate *localNowDate = [dateFormatterUTC dateFromString:[dateFormatterLocal stringFromDate:[NSDate date]]];
NSString *dateStr = #"2012-07-16 07:33:01";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter1 dateFromString:dateStr];
NSLog(#"date : %#",date);
NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"UTC"];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:date];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:date];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;
NSDate *destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:date] autorelease];
NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
[dateFormatters setDateFormat:#"dd-MMM-yyyy hh:mm"];
[dateFormatters setDateStyle:NSDateFormatterShortStyle];
[dateFormatters setTimeStyle:NSDateFormatterShortStyle];
[dateFormatters setDoesRelativeDateFormatting:YES];
[dateFormatters setTimeZone:[NSTimeZone systemTimeZone]];
dateStr = [dateFormatters stringFromDate: destinationDate];
NSLog(#"DateString : %#", dateStr);
- (Courtesy: Yuvaraj.M)
You can use the localTimeZone class method of NSTimeZone class to get a relative time zone object that decodes itself to become the default time zone for any locale in which it finds itself.
SRC: https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/NSTimeZone_Class/Reference/Reference.html#//apple_ref/occ/clm/NSTimeZone/localTimeZone
If you need latitude and longitude: Get the official time zone database at http://www.iana.org/time-zones. Download tzdata2013g.tar.gz. There's a file in that archive named zone.tab that gives a lat/long for each time zone, for example:
CH +4723+00832 Europe/Zurich
The +4723+00832 indicates latitude = +47º23", longitude = +8º23"
(SRC:Get Timezone/country from iPhone)

Displaying time from Date string

I have a time string in the following format(ISO 8601):
start = "1900-01-01T11:30:00"
stop = 1900-01-01T21:30:00
From the above string I need to display time like this :
Starting Time : 11:30a.m
Stopping Time : 09:30p.m
For that I had created an NSDate using below code, But I think it displays time in GMT format.
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
date = [dateFormatter dateFromString:opening];
stopTime = date;
//Output of above code : Date = 1900-01-01 15:36:40 +0000
How can I obtain the Starting and Ending time string from above ISO 8601 time string? Do I need to depend on String operations rather than using iOS NSDateformatter?
Try this,
NSString *start = #"1900-01-01T11:30:00";
NSString *stop = #"1900-01-01T21:30:00";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *sDate = [dateFormatter dateFromString:start];
NSDate *eDate = [dateFormatter dateFromString:stop];
[dateFormatter setDateFormat:#"hh:mm a"];
start = [dateFormatter stringFromDate:sDate];
stop = [dateFormatter stringFromDate:eDate];
NSLog(#"Starting Time : %#", start);
NSLog(#"Stopping Time : %#", stop);
And the outputs will be
2014-02-12 10:55:13.931 [950:a0b] Starting Time : 11:30 AM
2014-02-12 10:55:13.932 [950:a0b] Stopping Time : 09:30 PM
Check this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *currentDate = [NSDate date];
NSString *iso8601valueStr = [dateFormatter stringFromDate:currentDate];

Resources