1

I am trying to convert date from string ,

Below is my code,

str_date = @"04/16/2011 00:00:00 AM";

NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
[dtF setDateFormat:@"MM/dd/yyyy HH:mm:ss a"];

NSDate *d = [dtF dateFromString:str_date];


NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSString *dateString = [dateFormat stringFromDate:d]; 
[dateFormat release];

return dateString;

Here the issue is, in the simulator it is working fine and converts string to date and date to string, but with Device, it is not able to convert string to date

In iPod retina it is not showing the date, but with iPhone 3gs it shows the date

Please help me what wrong I have made ?

Costique
  • 23,712
  • 4
  • 76
  • 79
V.V
  • 3,082
  • 7
  • 49
  • 83
  • You might be running into [this issue](http://stackoverflow.com/questions/6613110/what-is-the-best-way-to-deal-with-the-nsdateformater-locale-feature) -- the phone behaves oddly if the 12/24 hour setting in Settings is in conflict with your locale. – Hot Licks Apr 16 '12 at 15:29
  • I have resolved issue , I just removed the time from the string and then removed the HH:mm:ss a , it's nowworking, I thing there were some issues similar as informed by Hot Licks. – V.V Apr 17 '12 at 05:43

1 Answers1

1

Just replace this line like this

NSDateFormatter *dtF = [[NSDateFormatter alloc] init]; [dtF setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];

Because HH use for 24 Hour format and hh use for 12 hour format...

Maulik
  • 19,348
  • 14
  • 82
  • 137
Nilesh Kikani
  • 2,628
  • 21
  • 37