iOS convert date to local time zone? - ios

I know this is a very question and asked multiple times before.But this is something i get stuck every time.
Now, i want to convert a date string from web service which is in Argentina local zone(UTC-3:0) and want to convert this that to device local time zone(Suppose UTC+5:30). Here is my code
-(NSDate *)getLocaDateStringFromDate:(NSString *)sourceDate andSourceTime:(NSString *)sourceTime
{
//sourceDate is #"2015-10-16"; and sourceTime is #"00:00:00"
static NSDateFormatter* df = nil;
if (!df) {
df = [[NSDateFormatter alloc]init];
df.dateFormat = #"yyyy-MM-dd HH:mm:ss";
}
NSString* source = [sourceDate stringByAppendingString:[NSString stringWithFormat:#" %#", sourceTime]];
NSTimeZone *sourceZone = [NSTimeZone timeZoneWithAbbreviation:#"ART"];
[df setTimeZone: sourceZone];
NSDate *ds = [df dateFromString:source];
NSLog(#"sourceZone time is %#" , [df stringFromDate: ds]);//sourceZone time is 2015-10-16 00:00:00, correct!
NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
[df setTimeZone: localTimeZone];
NSLog(#"local time is %#" , [df stringFromDate: ds]);//local time is 2015-10-16 08:30:00,, correct
NSString* str = [df stringFromDate: ds];
return [df dateFromString:str]; //this return 2015-10-16 03:00:00 +0000, why?
}
My question is that why my method return date in UTC, regardless of my time zone set to systemTimeZone. I get correct string but date is incorrect.
Any help would be appreciated.

1.) First, create an NSDateFormatter to get the NSDate sent from the server.
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
2.) Convert the date string to a NSDate.
NSDate* sourceDate = [dateFormatter dateFromString:dateString];
3) Specify ,Local and Destination timezone in which you want to convert your date.
For getting timezone of your source date:
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
For getting User timezone :
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
4.) Calculate the inverval between source timezone and user timezone as given bellow:
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
5.) Now, create an NSDateFormatter for formatting date in which you want to show.
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setDateFormat:#"M/d/yy 'at' h:mma"];
NSString* localTime = [dateFormat stringFromDate:destinationDate];
NSLog(#"localTime:%#", localTime);
Hope it is useful for you :)

Related

iOS NSDate get timeZone specific date?

I'm getting time in HH:mm:ss format from web service and it is in Argentina(GMT-3) time zone. I want to convert this time into full date (dd-MM-yyyy HH:mm:ss) and finally convert it into device local time zone's date.
Here is my code
-(NSString *)getLocalTimeStringFrom:(NSString *)sourceTime
{
static NSDateFormatter* df = nil;
static NSDateFormatter* df1 = nil;
if (!df) {
df = [[NSDateFormatter alloc]init];
df.dateFormat = #"dd-MM-yyyy HH:mm:ss";
}
if (!df1) {
df1 = [[NSDateFormatter alloc]init];
df1.dateFormat = #"dd-MM-yyyy";
}
NSTimeZone *sourceZone = [NSTimeZone timeZoneWithAbbreviation:#"ART"];
[df setTimeZone: sourceZone];
[df1 setTimeZone: sourceZone];
NSString *artDate = [df1 stringFromDate:[NSDate date]]; // get 29-09-2015
NSString* timeStamp = [artDate stringByAppendingString:[NSString stringWithFormat:#" %#",sourceTime]]; // get 29-09-2015 00:05:00, if sourceTime is 00:05:00
NSDate *art = [df dateFromString:timeStamp];
NSLog(#"ART date %#" , art);//ART date 2015-09-29 03:05:00 +0000
NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
[df setTimeZone: localTimeZone];
NSLog(#"Local date %#" , [df stringFromDate: art]);
return [df stringFromDate: ds];
}
The problem is that i'm getting UTC date in art (as commented in code),please note that the value of art is 2015-09-29 03:05:00 +0000 which is in UTC and format is yyyy-MM-dd HH:mm:ss, but is should be in ART and dd-MM-yyyy HH:mm:ss. I tried some code from net but didn't get solution. What is wrong in this code?
I use this:
NSDateFormatter* serverDf = [[NSDateFormatter alloc] init];
[serverDf setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:-3*60*60]];
[serverDf setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
NSDate* gmtDate = [serverDf dateFromString:timeStamp];
// DDLogVerbose(#"DateTime in GMT: %#", gmtDate);
NSDateFormatter* localDF = [[NSDateFormatter alloc] init];
[localDF setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
[localDF setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
NSString *localDate = [localDF stringFromDate:gmtDate];
May you just want a formate NSString , but not a NSDate,
Because I think NSDate will add a '+0000'
Try this.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:yourString];
NSString *str = [dateFormatter stringFromDate:date];

iOS convert server time to device local time?

I'm getting some issue to convert server time(argentina) to device local time.
here is my current code-
-(NSString *)getLocalTimeStringFrom:(NSString *)sourceTime
{
static NSDateFormatter* df = nil;
if (df == nil)
{
df = [[NSDateFormatter alloc]init];
}
df.dateFormat = #"HH:mm:ss";
NSDate* d = [df dateFromString:sourceTime];
NSTimeZone *sourceZone = [NSTimeZone timeZoneWithAbbreviation:#"ART"];//America/Argentina/Buenos_Aires (GMT-3)
NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone]; //Asia/Kolkata (IST)
[df setTimeZone: sourceZone];
NSLog(#"sourceZone time is %#" , [df stringFromDate: d]);
[df setTimeZone: localTimeZone];
NSLog(#"local time is %#" , [df stringFromDate: d]);
NSLog(#"original time string was %#" , sourceTime);
return [df stringFromDate: d];
}
And here is the log if sourceTime string is 00:05:00
2015-09-28 15:04:24.118 DeviceP[230:17733] sourceZone time is 15:35:00
2015-09-28 15:04:24.121 DeviceP[230:17733] local time is 00:05:00
2015-09-28 15:04:33.029 DeviceP[230:17733] original time string was 00:05:00
note that i'm getting local time same as the time string i pass into the method. i looked various SO post like this and this.
Any help would be appreciated.
Since your time string is in ART, you should set the timezone of date formatter before making date from the string. Means like following
-(NSString *)getLocalTimeStringFrom:(NSString *)sourceTime
{
static NSDateFormatter* df = nil;
if (!df) {
df = [[NSDateFormatter alloc]init];
df.dateFormat = #"HH:mm:ss";
}
NSTimeZone *sourceZone = [NSTimeZone timeZoneWithAbbreviation:#"ART"];
[df setTimeZone: sourceZone];
NSDate *ds = [df dateFromString:sourceTime];
NSLog(#"sourceZone time is %#" , [df stringFromDate: ds]);
NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
[df setTimeZone: localTimeZone];
NSLog(#"local time is %#" , [df stringFromDate: ds]);
return [df stringFromDate: d];
}

How to get current local time in ios

I'm try to get local time from device
nsdate will return the UTC time
when I convert with localtime zone the string will return current local time...
again I try to convert string to local date but it will return only UTC time
This is my code... how can I get Local time in NSDate...
NSDate *currentDate = [NSDate date];
NSLog(#"currentDate: %#", currentDate); // Result: currentDate: 2014-05-19 05:21:50 +0000
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter2 setDateFormat:#"yyyy/MM/dd HH:mm:ss"];
NSString *strDate = [dateFormatter2 stringFromDate:currentDate];
NSLog(#"strDate: %#", strDate); // Result: strDate: 2014/05/19 10:51:50
NSDate *newDate = [dateFormatter2 dateFromString:strDate];
NSLog(#"newDate: %#", newDate); // Result: newDate: 2014-05-19 05:21:50 +0000
can any one help to get local datetime in NSDate
Try this:
NSDate* sourceDate = [NSDate date];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
The time is always saved in NSDate as UTC, that can not be changed.
One can get a string representation for the local timezone, the OP has done that.
NSDate has a reference date of "the first instance of 1 January 2001, GMT"

Convert date from timezone to device local timezone and date

How can I convert date with timezone1 to date with device local timezone2?
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Asia/Jerusalem"]];
NSDate *date = [dateFormat dateFromString:timestamp];
then something like:
[dateFormat2 setTimeZone:[NSTimeZone localTimeZone]];
date2 = [dateFormat2 dateFromDate:date withOtherTimeZone:zone];
UPDATE:
I think I got it.
//get source date from the server
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
NSDate *sourceDate = [dateFormat dateFromString:timestamp];
//set timezones
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:#"Asia/Jerusalem"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
//calc time difference
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
//set current real date
NSDate* date = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
Is it ok?
OK first off, time is time. At this very second, everywhere on earth, its is the same second. Stop thinking of time format as time. It's not. All time should be in GMT, and then you can display it based on TimeZone.
Date *d = [NSDate date] ;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss Z"];
NSTimeZone *nyTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"sometimeabbr."];
NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
[df setTimezone: nyTimeZone];
NSLog(#"ny time is %#" , [df stringFromDate: d]);
[df setTimeZone: localTimeZone];
NSLog(#"local time is %#" , [df stringFromDate: d]);
If you are given a string date and need to convert, create NSDateFormatter to ingest the string date. Make sure you set a timezone. Now you have a NSDate object (no timezone in NSDates), that yiou can format into any other timezone you want.
Please for the love all all that is holy. Treat time as seconds that marches forward, and timezones as the formatting of those timezones.
This is the answer:
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:#"Asia/Jerusalem"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
//calc time difference
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
//set current real date
NSDate* date = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
*Different time zone date convert to local time zone date in swift.
let timeZone1 = NSTimeZone(name: timeZone1Name)
let localTimeZone = NSTimeZone.localTimeZone()
let timeZone1Interval = timeZone1?.secondsFromGMTForDate(timezone1Date!)
let deviceTimeZoneInterval = localTimeZone.secondsFromGMTForDate(timezone1Date!)
let timeInterval = Double(timeZone1Interval! - deviceTimeZoneInterval)
let originalDate = NSDate(timeInterval: timeInterval, sinceDate: timezone1Date!)
let dateFormater : NSDateFormatter = NSDateFormatter()
dateFormater.dateFormat = "YYYY-MM-dd HH:mm:ss Z"
NSLog("Converted date: \(dateFormater.stringFromDate(originalDate))")
let dateString = dateFormater.stringFromDate(originalDate)
let localTimeZoneDate = dateFormater.dateFromString(dateString)
*

formatting NSDate object changes the time zone

I get the current NSDate in the user time zone with the following code
-(NSDate *)getCurrentDateinLocalTimeZone
{
NSDate* sourceDate = [NSDate date];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] ;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
return [dateFormatter dateFromString: [dateFormatter stringFromDate:destinationDate]];
}
and at some other point in my app i want to format the date as "HH:mm" for UI purposes so i use the following method
-(NSString *)formatDate:(NSDate *)date
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
dateFormatter.dateFormat = #"HH:mm";
return [dateFormatter stringFromDate:date];
}
if the output of the second method is shifted by 3 hours from the result of the 1st method ,,, i only want to change the format of NSDate not the time , what am i doing wrong ?
The getCurrentDateinLocalTimeZone method adjusts the date for the time zone, formats it using a format string that chops off the time zone, and parses the formatted string back. The resulting NSDate is in the UTC time zone (the +0000 in 2012-07-15 16:28:23 +0000 indicates UTC time). The formatDate: method uses dateFormatter that is set up for the local time zone, producing a different time. You should set the formatter to use UTC to get the correct time: replace
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
with
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
in the formatDate: method.

Resources