NSDateFormatter returning nil - objective-c

can anyone please tell me why this code snippet returns nil in my log file:
NSString *dateString = [[NSString alloc] initWithString: #"2011-08-23 15:00"];
NSLocale *englishLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY-mm-dd HH:MM"];
[dateFormatter setLocale:englishLocale];
NSLog(#"%#",[dateFormatter dateFromString:dateString]);
Thanks a lot.

Check my variant:
NSString *dateString = [[NSString alloc] initWithString: #"2011-08-23 15:00"];
NSLocale *englishLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//HH:MM -> HH:mm
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
[dateFormatter setLocale:englishLocale];
NSLog(#"%#",[dateFormatter dateFromString:dateString]);
As you see I've changed date format to : #"yyyy-MM-dd HH:mm"
All formats can be found here: UNICODE LOCALE DATA MARKUP LANGUAGE (LDML)

you should be correct 'HH:MM' to 'HH:mm'
NSString *dateString = [[NSString alloc] initWithString: #"2011-08-23 15:00"];
NSLocale *englishLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//HH:MM -> HH:mm
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
[dateFormatter setLocale:englishLocale];
NSLog(#"%#",[dateFormatter dateFromString:dateString]);

http://www.picksourcecode.com/ps/ct/161110.php
For more dateFormats

Related

NsDateFormatter format the date

How can i this format this date ?
2/24/2017 11:13:43 AM
i tried this code but not worked
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
NSString *stringDate = [dateFormatter stringFromDate:createdDate];
You need to change the dateformate. You need to 'a' into formater.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss aa"];
NSString *stringDate = [dateFormatter stringFromDate:[NSDate date]];
Output:
Printing description of stringDate:
02/25/2017 09:56:42 AM
You can see more about dateFormate.
Cheers

Date format changing issue Objective C

All,
I want date format in dd/MMM/yyyy format below is the code i am using.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:#"dd/MMM/yyyy"];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
But in the dateString i am getting in Oct 21,2014 which is diffrent how can i get in "21/Oct/2014" format please let me know
The issue is that you're calling setDateStyle: instead of setDateFormat:
Use this code
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MMM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:[NSDate date]];
NSLog(#"%#", dateString);
This code outputs "22/Oct/2014"

Retrieve Year and Month from string of date

I want to retrieve year and then month from this kind of date: 2011-12-23 10:45:01 with no luck.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormatter setDateFormat:#"yyyy"];
NSLog(#"Date = %#",[dateFormatter dateFromString:#"2011-12-23 10:45:01"]);
[dateFormatter release];
Date = (null), i can't understand why.
You have to do it in two steps, first match the whole date, then output the bits you want:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:#"2011-12-23 10:45:01"];
//now you have the date, you can output the bits you want
[dateFormatter setDateFormat:#"yyyy"];
NSString *year = [dateFormatter stringFromDate:date];
[dateFormatter setDateFormat:#"MM"];
NSString *month = [dateFormatter stringFromDate:date];
[dateFormatter release];
This was already answered in Formatting NSDate into particular styles for both year, month, day, and hour, minute, seconds.
Specifically:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"HH:mm:ss"];
NSDate *now = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:now];
NSString *theTime = [timeFormat stringFromDate:now];
NSLog(#"\n"
"theDate: |%#| \n"
"theTime: |%#| \n"
, theDate, theTime);
[dateFormat release];
[timeFormat release];
[now release];

Converting to NSDate from NSString

I am trying to convert a Nsstring object to Nsdate.My code is like this
NSString *Time=#"09/26/2011 2:06 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehaviorDefault];
//Time=[mystr substringToIndex:3];
[dateFormatter setDateFormat:#"MM-DD-YYYY HH:MM"];
NSDate *dateFrom = [dateFormatter dateFromString:Time];
NSLog(#"\n\n\n\nThe date==%#\n\n\n\n\n",dateFrom);
But It returns a null value..Any help is Appreciated .Thanks in advance
Try to replace:
[dateFormatter setDateFormat:#"MM-DD-YYYY HH:MM"];
with
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm a"];

Date with Month name Objective-C

I have an NSDate object with value for example: "2011-08-26 14:14:51", I want to display "8 August 2011" how to implement it? Thanks.
NSDate *date = [NSDate date]; //I'm using this just to show the this is how you convert a date
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateStyle:NSDateFormatterLongStyle]; // day, Full month and year
[df setTimeStyle:NSDateFormatterNoStyle]; // nothing
NSString *dateString = [df stringFromDate:date]; [df release];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"dd LLLL yyyy"];
NSString *formattedDate = [df stringFromDate:[NSDate date]];
NSString *dateStr = #"2011-08-26 14:14:51";
//if you have date then,
NSString *dateStr = [NSString stringWithFormat:#"%#",yourDate];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat setDateFormat:#"MMMM dd, YYYY"];
NSString* temp = [dateFormat stringFromDate:date];
[dateFormat release];
NSLog(#"%#",temp);