I have string "24 Jan 2012 09:21:21 +0000" and I want to make it a NSDate object as
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate* date = [dateFormatter dateFromString:dateStr];
NSLog(#"%#",date);
gives me (null)
What i should do to get date in the format I want.
Since you have a specific date format, you can set your formatter up to match it.
dateString = #"24 Jan 2012 09:21:21 +0000";
[formatter setDateFormat:#"d MMM yyyy HH:mm:ss Z"];
date = [formatter dateFromString:dateString];
NSLog(#"Date: %#", date);
You can see the list of date specifiers here.
24 Jan 2012 09:21:21 +0000
[dateFormatter setDateFormat:#"dd MMM yyyy HH:mm:ss ZZZ"];
I don't have an obj C environment to verify, but your date string is "year month day" and the input you want to handle is "day (short)month year"
Related
I am trying to create a date out of my date string that goes:
Tue, 29 Jul 2014 17:22:30 +0200
I am using this code but it still gives me null as I try to dateFromString
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd LLL YYYY hh:mm:ss +0200"];
NSLog(#"The date: %#", [dateFormatter dateFromString:bazosItem.itemDate]);
so the output is:
The date: (null)
I have been searching for the right dateFormat in the internet and have rechecked it multiple times and I have no idea what is going wrong with the date.
There are some mistakes in your format, first the YYYY should be yyyy second the hh is for AM/PM hour not 24 hours for this you should use HH.
Third and most important you should the the date format in which language the date is by adding a locale:
NSString *temp = #"Tue, 29 Jul 2014 17:22:30 +0200";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setDateFormat:#"EEE, dd LLL yyyy HH:mm:ss +0200"];
NSLog(#"The date: %#", [dateFormatter dateFromString:temp]);
Your date format seems to be incorrect. Try the following
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss +0200"];
I always have trouble with this, and keep seem to find a good resource to learn this exhaustively. I am trying to use the date formatter in Xcode objective C, but I am not setting the date format correctly. Here is my data:
Fri, 27 Jun 2014 12:33:18 +0000
Can someone assist me or point me in the right direction? Trying the below code, but it is not working.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[dateFormatter setDateFormat:#"EEE, d LLL yyyy HH:mm:ss Z"];
Update: the format below might help, as it shows some zero padding on the day.
Wed, 02 Jul 2014 11:47:35 +0000
According to the Unicode Technical Standard #35, "LLL" is the date format for a "stand-alone month" which is "a month name without an associated day number".
You should use "MMM" instead:
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss Z"];
Example:
NSString *str = #"Fri, 27 Jun 2014 12:33:18 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:str];
NSLog(#"%#", date);
// Output: 2014-06-27 12:33:18 +0000
I have a problem with NSDateFormatter parsing a string coming from the web.
I parse dates as string and transform them to NSDate. The problem is in choosing the correct format string.
Parsed dates have following "format":
Feb 04, 2014 8:00 AM ET
but I haven't find the correct format to transform them into an NSDate
I've tried with: EEE dd, yyyy hh:mm a but it is not working.
Code is simple:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE dd, yyyy hh:mm a"];
NSDate *articleDate = [formatter dateFromString:articleDateString];
Any idea?
Try below code to convert your NSString to NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy hh:mm a 'ET'"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT+0:00"]];
NSDate *articleDate = [dateFormatter dateFromString:#"Feb 04, 2014 8:00 AM ET"];
NSLog(#"-->%#",articleDate);
Using this page: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Change your "EEE" to "MMM" and add a "z" at the end to catch your timezone.
"EEE" is day of week, for example, "Tue", not month.
I'm new to xcode, pls see below code and link
enter link description here
{
NSString * TempDate = [NSString stringWithFormat:#"%# %# %#",Datelbl.text,yearString,TimeLbl.text]; //Thursday, November 21 2013 5:35 PM
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat :#"EEEE',' MMMM d yyyy hh:mm a"];
AppointmentDate = [dateFormatter dateFromString:TempDate]; //NSDate
Appointment = [dateFormatter stringFromDate:AppointmentDate]; //NSString
}
Here , appointmentDate returns this value -> 2013-11-21 12:05:00 +0000,
Appointment returns this value-> Thursday, November 21 2013 05:35 PM.
I need to get 2013-11-21 05.35 PM. So,how to manage this GMT issue.
Try this one:
NSString *date = #"Thursday, November 21 2013 3:05 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat : #"EEEE, MMMM d yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *AppointmentDate = [dateFormatter dateFromString:date];
NSLog(#"DATE--> %#",AppointmentDate);
Output:
DAT--> 2013-11-21 15:05:00 +0000
Note: NSDate will be in 24 hour format. You need to convert it into string is you want as 03:05...
Edit :
[dateFormatter setDateFormat : #"EEEE, MMMM d yyyy hh:mm a"];
String value not converting to date format
This question already has answers here:
-[NSDateFormatter dateFromString:] returns nil
(3 answers)
Closed 5 years ago.
I am having problems with NSDateFormatter's dateFromString method.
I have the following date: Tue, 12 Jul 2011 20:18:26 GMT , but NSDateFormatter doesn't seem to recognize it and returns (null).
Here's what I tried:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:MM:SS"];
NSDate *date = [dateFormatter dateFromString:#"Tue, 12 Jul 2011 20:18:26 GMT"];
[dateFormatter release];
NSLog(#"Date: %#",date);
What am I doing wrong?
In this case you need to set dateFormat to match the format of the date in the string. Try following:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"ddd, dd MMM YYYY HH:MM:SS ZZZ"];
NSDate *date = [dateFormatter dateFromString:#"Tue, 12 Jul 2011 20:18:26 GMT"];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:MM:SS"];
NSString *newFormattedDate = [dateFormatter stringFromDate:date];
NSLog(#"Date: %# || %#",datem newFormattedDate);
Your problem is that your date format #"dd/MM/yyyy HH:MM:SS" doesn't match your date string #"Tue, 12 Jul 2011 20:18:26 GMT". You need to use a different date format.
The string you are parsing does not match the format string that you set on your NSDateFormatter. The format you entered is:
dd/MM/yyyy HH:MM:SS
...which matches strings like:
"15/07/2011 10:30:01"
If you want it to match Tue, 12 Jul 2011 20:18:26 GMT, then you need to change the format. You might have better luck with:
[dateFormatter setDateFormat:#"E, dd MMM yyyy HH:MM:SS z"];
You may find this needlessly complex documentation helpful.