Idrees
Idrees

Reputation: 133

NSDateFormatter for day and month name

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]);

Upvotes: 8

Views: 15224

Answers (5)

mithlesh jha
mithlesh jha

Reputation: 343

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);

Upvotes: 0

Ravi
Ravi

Reputation: 2451

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

Upvotes: 22

Agent Chocks.
Agent Chocks.

Reputation: 1312

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);

Upvotes: 0

Andrea
Andrea

Reputation: 26385

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.

Upvotes: 0

Huy Nghia
Huy Nghia

Reputation: 986

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

Upvotes: 0

Related Questions