Yuvrajsinh
Yuvrajsinh

Reputation: 4584

Convert string date to NSDate

I am doing simple string to date and vice versa conversion in my app but string is not converting properly to NSDate in iOS6 only in other versions of iOS it is working as expected.

Following is my code.

NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"dd MMMM, yyyy EEEE"];
    [df setLocale:[NSLocale currentLocale]];

    NSLog(@"String From Date : %@",[df stringFromDate:[NSDate date]]);

    NSDate *today = [df dateFromString:@"29 January, 2014 Wednesday"];
    NSLog(@"Date : %@",today);
    NSLog(@"Date From String : %@",[df stringFromDate:today]);

My output in iOS 6.0 is :

String From Date : 29 January, 2014 Wednesday
Date : 1999-12-28 18:30:00 +0000
Date From String : 29 December, 1999 Wednesday

My Output in iOS7 is :

String From Date : 29 January, 2014 Wednesday
Date : 2014-01-28 18:30:00 +0000
Date From String : 29 January, 2014 Wednesday

I have spend much time on searching the date conversion but this is something strange issue i am getting. Can any one help me out with this issue….

Thanks for your support.

Upvotes: 3

Views: 604

Answers (1)

Mohammad Allam
Mohammad Allam

Reputation: 258

I think the problem of iOS6 is in the data formatter style ,all what you have to do is to replace that line

[df setDateFormat:@"dd MMMM, yyyy EEEE"];

with that one

[df setDateFormat:@"EEEE dd MMMM, yyyy"];

Upvotes: 1

Related Questions