Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

NSDateFormatter @"mmddyyy"?

Helo, I'm trying the bunch of code and it is giving null. Is there any problem in this code?

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    // get NSDate from old string format
    [dateFormatter setDateFormat:@"mmddyyy"];
    NSDate *date = [dateFormatter dateFromString:@"02012002"];
    NSLog(@"%@",date);
    // get string in new date format
    [dateFormatter setDateFormat:@"dd-MMM-yyyy"];
    NSString *strMyDate= [dateFormatter stringFromDate:date];
    NSLog(@"%@\n\n********************************************************",strMyDate);

the output is

2011-06-09 12:49:17.957 TestPrj[3054:207] (null)
2011-06-09 12:49:17.959 TestPrj[3054:207] (null)

********************************************************

Upvotes: 1

Views: 171

Answers (2)

Nick Weaver
Nick Weaver

Reputation: 47241

Please try

[dateFormatter setDateFormat:@"MMddyyyy"];

EDIT

In my previous version DD did work in this example though it will produce wrong results beginning with february -> back to dd.

Use MM for the months else it'll parse it as minutes.

Please refer to the Unicode Technical Standard #35 / Locale Data Markup Language (LDML).

Upvotes: 2

Mahesh
Mahesh

Reputation: 662

You r missing one 'y' there in second line- it either should be -

dateFormatter setDateFormat:@"mmddyyyy"];

or dateFormatter setDateFormat:@"MMddyyyy"];

Upvotes: 1

Related Questions