Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]);
like image 497
Idrees Avatar asked Mar 09 '15 06:03

Idrees


People also ask

What format is my date string?

The string format should be: YYYY-MM-DDTHH:mm:ss. sssZ , where: YYYY-MM-DD – is the date: year-month-day.

What is en_US_POSIX?

en_US_POSIX was invented to refer to the C (or 'null') locale code used in POSIX libraries.

How do I change the date format in swift 5?

dateFormat = "yyyy-MM-dd'T'HH:mm:ss. SSS'Z'" dateFormatterGetNoMs. dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'" let dateFormatterPrint = DateFormatter() dateFormatterPrint. dateFormat = "MMM dd,yyyy" for dateString in isoDateArray { var date: Date?

Is DateFormatter thread safe iOS?

Thread SafetyOn iOS 7 and later NSDateFormatter is thread safe. In macOS 10.9 and later NSDateFormatter is thread safe so long as you are using the modern behavior in a 64-bit app.


1 Answers

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
like image 67
Ravi Avatar answered Oct 20 '22 19:10

Ravi