NSDateFormatter and Time Zone issue? - iphone

I have a string with time in GMT and i want to make it according to the system time zone but its not working properly -
NSLog(#"Time Str = %#",Time);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy hh:mm a"];
[NSTimeZone resetSystemTimeZone];
NSLog(#"system time zone = %#",[NSTimeZone systemTimeZone]);
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date = [dateFormat dateFromString:Time];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(#"date from string = %#",date);
NSLog(#"string from date = %#",[dateFormat stringFromDate:date]);
Output at console -
////////////////////////////////////////////////////////////////////////////////////////////////
Time Str = 09-12-2011 07:57 AM
system time zone = Asia/Calcutta (IST) offset 19800
date from string = 2011-12-09 02:27:00 +0000
string from date = 09-12-2011 07:57 AM
////////////////////////////////////////////////////////////////////////////////////////////////
Calcutta is +5:30 from GMT, so the time in date should be 1:27 but its showing 02:27. Also when i take a string from this date its showing me the same sting that i used to make date, i want the string updated according system time zone.
Thanks

If the date string is in GMT you can't use your system Timezone to create the NSDate from the NSString.
replace the first occurrence of [dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
with [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];

Related

Convert GMT time to local time

I got current GMT time - 2013-05-15 08:55 AM
my current local time is - 2013-05-15 02:06 PM
and time zone is - Asia/Kolkata
I tried to convert the above GMT to my local time with this code
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"Asia/Kolkata"];
[dateFormatter setTimeZone:sourceTimeZone];
// timeStamp - is the above GMT time
NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
NSLog(#"local time is %#",dateFromString);
but this give me wron time as - 2013-05-14 19:04:00 +0000 instead of 2013-05-14 02:06 PM
Why this happens?
please help me to clear this.Thanks in advance
Nothing is going wrong, a NSDate instance will never have a timezone ( well it will but it is always GMT, indicated by the +0000 timezone in the date description).
What you are telling dateformatter in your code is that the timezone of the date you want to parse is Asia/Kolkata but you want it to be GMT.
First you need to make a NSDate object from the input date string with the time zone set to the correct time zone, GMT as you indicated.
NSTimeZone *gmtTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
NSLog(#"Input date as GMT: %#",dateFromString);
Then when you present the date as an string use :
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:#"Asia/Kolkata"];
[dateFormatter setTimeZone:sourceTimeZone];
NSString *dateRepresentation = [dateFormatter stringFromDate:dateFromString];
NSLog(#"Date formated with local time zone: %#",dateRepresentation);
The shortest way that I found to convert GMT time to your local timezone is
NSDate* localDateTime = [NSDate dateWithTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMT] sinceDate:dateInGMT];
Furthermore, to convert this date value into string
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];//use the formatted as per your requirement.
//Optionally for time zone converstions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"..."]];
NSString *localTimeDateString = [formatter stringFromDate:localDateTime];
NSLog(#"local time is %#",[dateFormatter stringFromDate: dateFromString]);
NSTimeZone* gmtTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSTimeZone* localTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"Asia/Kolkata"];
NSDateFormatter *gmtDateFormatter = [[NSDateFormatter alloc] init];
[gmtDateFormatter setTimeZone:gmtTimeZone];
NSDate *date = [gmtDateFormatter dateFromString:timestamp];
NSDateFormatter *localDateFormatter = [[NSDateFormatter alloc] init];
[localDateFormatter setTimeZone:localTimeZone];
NSLog(#"local time is %#",[localDateFormatter stringFromDate:date]);
Use timeZoneWithName
NSDate *date=[[NSDate alloc]init];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Asia/Kolkata"]];
[timeFormat setDateFormat:#"HH:mm"];
NSString *dateStr=[timeFormat stringFromDate:date];

NSDate from NSString adjusted to device timzone

I have an NSString like this :
NSString *playTime = #"20:45";// GMT
I need to convert this string to NSDate (only hours and seconds) then adjusted to device timezone.
Example:
If user device timezone is set to GMT, I need a result like this : #"20:45";
If user device timezone is set to GMT + 2, I need a result like this : #"22:45";
Try this
NSString *gmtDateString = #"20:45";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"HH:mm"];
//Now you have date in GMT time zone by default dateFormatter timezone would be in local time zone
NSDate *date = [dateFormatter dateFromString:dateString];
//set to local time zone
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
//Now you have string in localDate format
NSString *localDateString = [dateFormatter stringFromDate:date];
The below code will calculate the time w.r.t. local time zone.
Suppose my time zone is GMT+05:30 so the time 05:30 will become 00:00.
NSString *playTime = #"20:45";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"HH:mm";
NSDate *date = [formatter dateFromString:playTime];
NSLog(#"%#",date);
Log is: 2000-01-01 15:15:00 +0000
Use this code for date conversion as per your requirement.
NSDateFormatter *dateFormat = [NSDateFormatter new];
[dateFormat setDateFormat:#"HH:mm"];
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
logDate = [dateFormat dateFromString:#"20:45"];
NSLog(#"Date %#",[dateFormat stringFromDate:logDate]);

How to get my time in UTC for iPhone project?

How to get my country time in UTC for iPhone development?
Here is the Code, Just Call below method when you want to set TimeZone and Date Format.
-(void)setDateFormat
{
NsDate myDate = [NSDate date];//here it returns current date of device.
//now set the timeZone and set the Date format to this date as you want.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:timeZone];
NSString *newDate = [dateFormatter stringFromDate:myDate];
// here you have new Date with desired format and TimeZone.
}
-(NSString *)UTCFormDate:(NSDate *)myDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:timeZone];
NSString *dateString = [dateFormatter stringFromDate:myDate];
return dateString;
}
Source:
https://stackoverflow.com/a/2615847/857865
[NSDate date] gives you the current point in time, which may be printed in UTC, GMT, EST or any other timezone. (Representation-wise, the current point in time is the number of seconds since a reference date in UTC.)
To get the current point in time in UTC as a string, get an NSDateFormatter and configure it to output a timestamp using the UTC timezone.

Unable to format UTC date with NSDateFormatter

I have the following date:
2011-10-20T01:10:50Z
I would like it to be formatted to
"M/d/yy 'at' h:mma"
Here is my code:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
//The Z at the end of your string represents Zulu which is UTC
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy-dd-MM'T'HH:mm:ss'Z'"];
NSDate* newTime = [dateFormatter dateFromString:[message valueForKey:#"created_at"]];
//Add the following line to display the time in the local time zone
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"M/d/yy 'at' h:mma"];
NSString* finalTime = [dateFormatter stringFromDate:newTime];
[dateFormatter release];
NSLog(#"%#", finalTime);
Unfortunately the finalTime is NULL here.
You have dd-MM backwards. You have 10 for dd and 20 MM. There is no month 20.
2011-10-20T01:10:50Z
#"yyyy-dd-MM'T'HH:mm:ss'Z'"
Because of that, the newTime was null and the finalTime was null.
Here's with the fix. This:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
//The Z at the end of your string represents Zulu which is UTC
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate* newTime = [dateFormatter dateFromString:#"2011-10-20T01:10:50Z"];
NSLog(#"original time: %#", newTime);
//Add the following line to display the time in the local time zone
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"M/d/yy 'at' h:mma"];
NSString* finalTime = [dateFormatter stringFromDate:newTime];
NSLog(#"%#", finalTime);
[dateFormatter release];
Outputs:
2011-10-19 22:05:15.107 Craplet[4231:707] original time: 2011-10-20 01:10:50 +0000
2011-10-19 22:05:15.116 Craplet[4231:707] 10/19/11 at 9:10PM
This technical note helped me resolve the same issue: https://developer.apple.com/library/ios/qa/qa1480/_index.html
In a nutshell, you need to set 3 things on NSDateFormatter:
Locale
TimeZone
DateFormat
code sample:
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:locale];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'"];
NSDate *date = [dateFormatter dateFromString:#"2014-10-23T01:10:50.000Z"];
I was using NSDateFormatter to successfully parse UTC strings to NSDate by only setting time zone and date format. When it suddenly stopped working I found this tech note and added setLocal: which resolved the issue for me.
Because Swift:
let dateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
let dateHopefully = dateFormatter.dateFromString("2016-07-29T17:15:02Z")
dateFormatter.timeZone = NSTimeZone.systemTimeZone()
dateFormatter.dateFormat = "M/d/yy 'at' h:mma"
let lastTime = dateFormatter.stringFromDate(dateHopefully!)
print("Here is the hopeful date \(dateHopefully) and here is the lastTime \(lastTime)")

Problem in setting nsdate value from a string

NSDate *My_StartDate,*My_EndDate ;
NSDateFormatter * df= [[NSDateFormatter alloc]init];
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
My_StartDate = [df dateFromString:#"01/05/2010 10:15:33"];
My_EndDate = [df dateFromString:#"01/05/2010 10:45:33"];
NSLog(#"%#",My_StartDate);
NSLog(#"%#",My_EndDate);
In the log i get something like this for the my_startdate as 2010-05-01 04:45:33 +0000 and end date as 2010-05-01 05:15:33 +0000 instead i should have got value as for start date as 2010-05-01 10:15:33 +0000 and end date as 2010-05-01 10:45:33 +0000
Try with below function:
-(NSString *)getDateStringFromDate :(NSDate *)dateValue{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setTimeStyle:NSDateFormatterShortStyle];
[timeFormat setDateFormat:#"HH:mm:ss"];
//[timeFormat setDateFormat:#"HH:mm"];
//[timeFormat setDateFormat:#"HH:mm a"];
////
NSString *theDate = [dateFormat stringFromDate:dateValue];
NSString *theTime = [timeFormat stringFromDate:dateValue];
NSLog(#"\n"
"theDate: |%#| \n"
"theTime: |%#| \n"
, theDate, theTime);
return theDate;
}
Change Format of data as per your need.
Let me know in case of any difficulty.
Cheers.
This shows date which follow American standard time string but by this reason you don't get any problem in making your logic.Also
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
this format using 12 hour format (means 2:03 pm and 2:03 am) and date object never use am and pm for showing date object value but when you convert it correctly then it gives you right date and time.
If you feel you get any problem then use different locale for that.
It is displaying asper the GMT+4.30 time.It displays like that only.When you are converting that date to string using the DateFormatter it gives the same date(Whichever you want like start date as 01/05/2010 10:15:33 and end date as 01/05/2010 10:45:33).
NSDateFormatter * dateformatter= [[NSDateFormatter alloc]init];
[dateformatter setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
NSString *dat = [dateformatter stringfromDate:My_StartDate];
then you will get the output as 01/05/2010 10:15:33
You might want to set the time zone of the date formatter to GMT here. Do it using
[df setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
before you do dateFromString: calls. This will give you what you want.
Just need to update here in your code:
I might be like that your time would be in 24 hours format, so at that time you need to use this ....other than that you need to set the timezone.
Follow this link for All zone : http://unicode.org/reports/tr35/tr35-6.html#Date%5FFormat%5FPatterns
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
to
[df setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
You are Done ;)