0

Below is what I am using

NSString *myDate = @"01-11-2014 10:22 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy hh:mm a"];
NSDate *newDate = [dateFormatter dateFromString:myDate];
NSLog(@"new date===%@=====%@", newDate, myDate);

Below is what I am getting

new date===2014-11-01 19:22:00 +0000=====01-11-2014 10:22 PM
                      ^^

Output I was expecting is

new date===2014-11-01 22:22:00 +0000=====01-11-2014 10:22 PM
                      ^^

Any idea what is going wrong?


When I have AM I have below output

new date===2014-11-01 07:22:00 +0000=====01-11-2014 10:22 AM
                      ^^

Edit 1

Actually what I am doing is asked date and time in UITextField (sadly but true as client wanted it in same way)... and then concatenating this string and converting it to NSDate.

So what I have is

 NSString myDate = [NSString stringWithFormat@"%@ %@", appDate, appTime];
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
  • 1
    Seems like it's a time zone issue to me. Does your myDate have a time zone? – Jamie Jul 26 '13 at 18:53
  • @Jamie : No, its just a string.. – Fahim Parkar Jul 26 '13 at 18:53
  • This isn't a time zone issue, it's correct. If you use `NSLog()`, the date's `-description` method will be used to format the date. And its implementation uses UTC, not the local time zone. The date you specify with your string probably is 19:22 in UTC. – Fabian Kreiser Jul 26 '13 at 18:54
  • Right..so you're getting the time from somewhere that already has the time zone calculated into it. – Jamie Jul 26 '13 at 18:54
  • @JoshCaswell : thanks for your many many many.... :D – Fahim Parkar Jul 26 '13 at 19:01
  • @Jamie : I was worried for NSLog, but the reminder in my app is going proerly... it was just NSDate formatter was using UTC and hence showing 3 hrs difference.. – Fahim Parkar Jul 26 '13 at 19:02

1 Answers1

0

Add this line of code:

[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

You probably had the wrong timezone. You have to set the timezone to your timezone.

Abdullah Shafique
  • 6,878
  • 8
  • 35
  • 70