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?
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 aware that this question is asked too many times but none matched my requirement.
I have "8-8-2015 12:00:00 AM" in NSString named strEventTimeBegin. How do I convert this NSString to NSDate? Here's what I tried but returns null.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
//[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSDate *startEventDate = [dateFormatter dateFromString:strEventTimeBegin];
From memory, but this should work:
[dateFormatter setDateFormat:#"d-M-yyyy hh:mm:ss a"];
Just to explain, in your example;
The day and the year are backwards.
You're using 2 digit days and
months (It's looking for 08-08-2015).
You're missing the period
(AM/PM) identifier.
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
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"];
I have set the locale and timezone but when I format the date, I always get invalid date. The month is always December and year is one less that the specified year. In my case I dont need the day component.
I checked other similar post but it didn't solved the problem.
Any help is appreciated. Thank you.
Here is the code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setDateFormat:#"MMM YYYY"];
NSDate *formattedDate = [dateFormatter dateFromString:#"Sep 2013"];
NSLog(#"Date: %#", [dateFormatter stringFromDate:formattedDate]);
[dateFormatter release]; dateFormatter = nil;
OUTPUT: Date: Dec 2012
"YYYY" format is used for "Week of Year" based calendars. Use "yyyy" format specifier instead:
[dateFormatter setDateFormat:#"MMM yyyy"];
I just wanted to add something to the great answer by Vladimir. If you do this before setting your locale, it seems that the date formatting goes crazy. What I had to do was to set the locale before setting the new format, and then use the setDateFormat to change the format based on the locale used.So something like this would do:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];//I wanted to set the locale to wherever user is using my app in
[formatter setDateFormat:#"MMM YYYY"];
NSDate *now = [NSDate Date];
NSString *fancyLookingDate = [formatter stringFromDate:now];