NSDateFormatter for day and month name

I am trying to get a date in the format Monday, March 09, 2015. But the following code does not return the required date format. I think I'm using the wrong Formatter. Here is the code:

NSString *dateString = @"09-03-2015"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd MMMM yyyy"]; NSDate *date = [[NSDate alloc] init]; date = [dateFormatter dateFromString:dateString]; NSLog(@"%@",[dateFormatter stringFromDate:date]); 
+7
ios objective-c nsdate nsdateformatter
source share
5 answers

try it...

 //Getting date from string NSString *dateString = @"09-03-2015"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MM-yyyy"]; NSDate *date = [[NSDate alloc] init]; date = [dateFormatter dateFromString:dateString]; // converting into our required date format [dateFormatter setDateFormat:@"EEEE, MMMM dd, yyyy"]; NSString *reqDateString = [dateFormatter stringFromDate:date]; NSLog(@"date is %@", reqDateString); LOG:2015-03-09 12:40:33.456 TestCode [1377:38775] date is Monday, March 09, 2015 
+20
source share

First, you must convert the dateString value to NSDate

 NSString *dateString = @"09-03-2015"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MM-yyyy"]; NSDate *date = [[NSDate alloc] init]; date = [dateFormatter dateFromString:dateString]; NSLog(@"%@",[dateFormatter stringFromDate:date]); 

after that you can use this format: @"EEEE,MMMM dd, yyyy" to convert this dateValue to dateString as Monday, March 09, 2015
Hope this help
Useful link for you

0
source share

Try setting the dateFormat property to @"dd'-'MM'-'yyyy" . Always check the unicode date character table . "MMMM" means that the month of the date must be the full name of the month.

0
source share

Try it.

  NSString * yourJSONString = @"09-03-2015"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];; [dateFormatter setDateFormat:@"dd-MM-yyyy"]; [dateFormatter setLocale:[NSLocale currentLocale]]; NSDate *dateFromString = [dateFormatter dateFromString:yourJSONString]; [dateFormatter setDateFormat:@"EEEE,LLLL dd, yyyy"]; NSString *output = [dateFormatter stringFromDate:dateFromString]; NSLog(@"%@", output); 
0
source share

Try the following: -

 NSDate *date = [NSDate date]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; NSString * format = [NSDateFormatter dateFormatFromTemplate:@"EEEEMMMMdyyyy" options:0 locale:[NSLocale currentLocale]]; [formatter setDateFormat:format]; NSString *dateFormatted = [fullDateFormatterTime stringFromDate: date]; NSLog(@"Formatted date: %@", dateFormatted); 
0
source share

All Articles