I am having trouble with date formatter. I am setting date format and passing the date string in the appropriate way (i think). But the result log shows some other dateand the GMT value has been lost. What am I doing wrong here ? Can anyone help me out here ?
NSDateFormatter *newFormatter = [[[NSDateFormatter alloc] init] autorelease];
[newFormatter setDateFormat:#"yyyy-mm-dd hh:mm:ss zzz"];
NSDate *lObjDate = [newFormatter dateFromString:#"2012-11-05 01:45:03 GMT+05:30"];
NSLog(#">>>>> %#",lObjDate);
>>>>> 2012-01-04 20:15:03 +0000
You may try:
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss Z"];
Related
I'm trying to create a date object with the specified formatter but date formatter_datefromstring method returns nil. Please let me know with the clear documentation samples. The String am trying to parse is "2:00 AM PDT on September 24, 2017". Thanks in advance
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:#"h:mm a Z 'on' MMMM d, yyyy"];
NSLog(#"dateStr:==============> %#", dateStr);
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"Date:------------->%#", date);
return date;
You are using the wrong timezone specifier. Z is for timezones such as -0800. You need z for short timezone abbreviations like PDT.
Also, there is no reason to set the formatter's local to currentLocale and the timezone to systemTimeZone since those are the defaults. And the timezone of the formatter is irrelevant when the string you are parsing contains timezone information.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"h:mm a z 'on' MMMM d, yyyy"];
NSLog(#"dateStr:==============> %#", dateStr);
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"Date:------------->%#", date);
return date;
However, since you are parsing a fixed format date string that is in English, you really should set the formatter's locale to the special locale of en_US_POSIX. This will ensure it handles the English month name no matter the user's locale.
[dateFormat setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
Can you check your dateStr date format and your given format same or not. If both are not same format you will get nil object. Try dateStr format in given below example.
NSString *dateStr = #"10:23 am Z on September 30, 2017";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:#"h:mm a Z 'on' MMMM d, yyyy"];
NSLog(#"dateStr:==============> %#", dateStr);
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"Date:------------->%#", date);
In console you will get
2017-09-23 09:54:04.654597+0530 Date[4068:79926] dateStr:==============> 10:23 am Z on September 30, 2017
2017-09-23 09:54:04.657359+0530 Date[4068:79926] Date:------------->Sat Sep 30 15:53:00 2017
Use the below locale the avoid returning nil value.
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
Vote my code if it is usefull
I am using following code to get date from string. All seems to be good in code but while I print the output date, there is difference of 12:30 hours in date.
What may be the issue? Am I missing something ?
NSString *strDate = #"8/22/2017 7:00:00 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
NSDate *date = [df dateFromString:strDate];
NSLog(#"%#", [date description]);
Output:
2017-08-21 18:30:00 +0000
The hour specifier is wrong, 12 hour format is hh
[df setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
Note:
Be aware that NSLog prints the date always in UTC although the date formatter considers the local time zone.
try this:
you just set the time to GMT
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
I googled so many times, but I am not satisfied with given answer. Please anyone can give correct answer what I need.
This is the retrieve date string from DB : 2015-05-27 10:19 (yyyy-MM-dd HH:mm)
I want to convert into NSDate.
My code is like Below:
NSString *date_str = #"2015-05-27 10:19";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:date_str];
NSLog(#"date == %#",date);
But output is : date == 2015-05-27 04:49:00 +0000
Its showing 04:49:00 time , but my retrieve time is 10:19.
How can i retrieve perfect time from DB.
Please help out me..
Just convert your date to GMT time like this:-
NSString *date_str = #"2015-05-27 10:19";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormat setTimeZone:gmt];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:date_str];
And the output is :-
date == 2015-05-27 10:19:00 +0000
And to get the date without +0000, you can store it in NSString directly:-
NSString *s = [dateFormat stringFromDate:date];
Output :-
2015-05-27 10:19
You are facing the timezone issue with conversion, because server time and local timezone may have difference. So handle this you need to set the timezone to your dateformatter like
[dateFormater setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
I found this weird issue, when converting from string to a NSDate. The final date is wrong by exactly 2 or 3 hours (possible an integer number of hours). My data has an excellent quality as it was picked from other ios devices programmatically. For example when I try to convert:
"2014-05-23 03:14:04 a.m. +0000"
I get:
2014-05-23 00:14:04 +0000
or, when converting:
"2014-05-23 02:49:30 a.m. +0000"
I get:
2014-05-23 00:49:30 +0000
The date format is in Spanish and therefore my code is:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss a ZZZ"];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"es"]];
[dateFormatter setAMSymbol:#"a.m."]; // default AM symbol for spanish is a.m.
[dateFormatter setPMSymbol:#"p.m."]; // default PM symbol for spanish is p.m.
// Set the date format for the input string
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss a ZZZ"];
newEvent.time = [dateFormatter dateFromString:[timeArray objectAtIndex:i]];
according to:
https://stackoverflow.com/a/13757666/2394901
but with a modification for a.m. and p.m. instead of AM and PM because that way the answer is nil.
UPDATE:
This issue is not due to time zone difference as suggested below. Instead the proper solution is:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"es"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ssa ZZZ"];
newEvent.time = [dateFormatter dateFromString:[timeArray objectAtIndex:i]];
**the problem is the capital letters HH, should be hh.
Anybody knows when should letters must by capitalized?
I have an NSString that is 05/08/2014. I want to convert that to an NSDate. However, I also need to add in time, so that the resulting NSDate looks like this:
Thu, 8 May 2014 00:00:00 -0500
The time is not important, I just need it to show midnight at the designated timezone.
I have tried:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss zzz"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:textdate.text];
[dateFormatter release];
NSLog(#"%#", dateFromString);
But the date comes back as (null).
Your date format is wrong for the first conversion. What you need is first to convert from string to date from one format and form the new date into the new string format. Something like this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"]; //convert the string into date (american time zone)
NSDate *theDate = [formatter dateFromString:textdate.text];
[formatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZ"];// as #Logan suggested
NSString *newDate = [formatter stringFromDate:theDate];
EEE, d MMM yyyy HH:mm:ss ZZZ
Your time zone code was lowercase instead of uppercase.
zzz corresponds to PDT
ZZZ corresponds to -0500
UTS 35