-1

I'm having some issues trying to format a string into NSDate. This is my code.

        NSTimeZone *outputTimeZone = [NSTimeZone defaultTimeZone];
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

        [dateFormat setTimeZone:outputTimeZone];
        [dateFormat setLocale:[NSLocale currentLocale]] ;

        [dateFormat setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
        NSDate *myDate = [dateFormat dateFromString:dateString];

this is the string that I'm trying to format.

"09/11/2016 12:27:05"

this is the result I get

2016-11-09 04:48:29 UTC

Please help I have been trying various solutions and formats but to no avail.


After the call to dateFromString, add the line NSLog("str = %@, date = %@", dateString, myDate);. Update your question with the output of that log statement. – rmaddy 59 mins ago

This is my output

str = 09/11/2016 14:13:35, date = 2016-11-09 06:13:35 +0000

Mavrik Teo
  • 211
  • 2
  • 6

1 Answers1

-1

Use timeZonewithAbbrevation instead of default timeZone.

Replace NSTimeZone *outputTimeZone = [NSTimeZone defaultTimeZone]; with

// For GMT

NSTimeZone *outputTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT+0:00"];

OR

// For UTC

NSTimeZone *outputTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
Daggarwal
  • 234
  • 1
  • 7
  • Thank you! That worked for me! I used timeZoneWithAbbreviation:@"UTC" and my output was str = 09/11/2016 14:32:29, date = 2016-11-09 14:32:29 +0000 the format is still different though but what matters is the time is correct! Thank you very much! – Mavrik Teo Nov 09 '16 at 06:30
  • 1
    No, it's not correct. You misunderstand. – rmaddy Nov 09 '16 at 06:42