Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 364
Yuvrajsinh Avatar asked Mar 21 '26 03:03

Yuvrajsinh


1 Answers

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"];
like image 143
Mohammad Allam Avatar answered Mar 22 '26 16:03

Mohammad Allam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!