8

I am trying to get date in the format Monday, March 09, 2015. But following code is not returning required date format. I think I am using 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]);
Idrees
  • 133
  • 1
  • 2
  • 6
  • change this line `[dateFormatter setDateFormat:@"dd MMMM yyyy"];` to `[dateFormatter setDateFormat:@"dd-MM-yyyy"];` – SGDev Mar 09 '15 at 07:01
  • There is no point creating a date and then replacing it with the result of the formatter. Also, log the result, not a string you then try to create from the result. – Wain Mar 09 '15 at 07:02
  • 1
    For the sake of your users, please read this: http://stackoverflow.com/a/5132177/457406 – Matthias Bauch Mar 09 '15 at 07:02
  • @SumitGarg but this doesn't return the required format like Monday, March 09, 2015 – Idrees Mar 09 '15 at 07:04
  • ok now used this date format `EEE, MMM dd, yyyy`. – SGDev Mar 09 '15 at 07:07

5 Answers5

22

try this...

//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
Ravi
  • 2,441
  • 1
  • 11
  • 30
0

Firstly you must convert your dateString to NSDatevalue

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 could use this format: @"EEEE,MMMM dd, yyyy" to convert that dateValue to dateString like Monday, March 09, 2015
Hope this help
Helpful link for you

Huy Nghia
  • 996
  • 8
  • 22
0

Try to set dateFormat property to @"dd'-'MM'-'yyyy" . Always check the unicode the date symbol table. "MMMM" means that the date month should be a full month name.

Andrea
  • 26,120
  • 10
  • 85
  • 131
0

Try this one..

    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);
Agent Chocks.
  • 1,312
  • 8
  • 19
0

Try this:-

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);
mithlesh jha
  • 343
  • 2
  • 11