简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM