1

My API sending me date string in UTC like 2/20/2016 11:45 AM. For displaying date in my timezone, I am converting date from UTC to Local time zone like below code

    let sourceDateString = "12/20/2016 11:45 AM"
    let formatter = DateFormatter()
     formatter.dateFormat = "MM/dd/yyyy hh:mm a"
     formatter.timeZone = TimeZone(identifier: "UTC")
     let date = formatter.date(from: sourceDateString)

     print(date)

     formatter.timeZone = NSTimeZone.local
     formatter.dateFormat = "MMM dd,yyyy hh:mm a"

     let strDate = formatter.string(from: date!)
     print(strDate)

Above code is working fine if my device time format is in 12 HR but if I am using 24 Hr date from my device then above code is not working. Event it's not able to convert UTC string to date.

Please check above code in the device, it's working fine in simulator.

Shobhakar Tiwari
  • 7,862
  • 4
  • 36
  • 71
Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39
  • 2
    Use `HH` instead of `hh`, `hh` is for `12` hour format and `HH` is for `24` hour format. – Sachin Vas Dec 22 '16 at 07:35
  • 1
    Set the date formatters locale to "Posix", compare http://stackoverflow.com/questions/40692378/swift-3-dateformatter-doesnt-return-date-for-hhmmss. – Martin R Dec 22 '16 at 07:44
  • @MartinR after assigning dateFormatter locale to "POSIX", the format needs to include "HH" instead of "hh" (as mentioned in your answer), right? – Ahmad F Dec 22 '16 at 07:49
  • @AhmadF: No, you can also set it to "hh a" *after* fixing the locale. Setting "Posix locale" is done to ignore the user's locale settings. – Martin R Dec 22 '16 at 07:50
  • @MartinR Ok I got it about the need of changing the locale, but what about that "HH" is the format for 24-hour, how should it works if "hh" is kept as is? sorry for bothering, but I really would like to understand what is going on here :) – Ahmad F Dec 22 '16 at 08:04
  • @AhmadF: The question (as I understand it) is about converting a date from UTC to the local time zone, and *not* about 12hr to 24hr conversion. – Martin R Dec 22 '16 at 08:19
  • @MartinR You are awesome man posix worked. Thanks for help you are genius. – Tejas Ardeshna Dec 22 '16 at 08:53

3 Answers3

3

With help of @Martin R, Following code worked for me.

    let sourceDateString = "12/20/2016 11:45 AM"
    let formatter = DateFormatter()
    formatter.dateFormat = "MM/dd/yyyy hh:mm a"
    formatter.timeZone = TimeZone(identifier: "UTC")
    formatter.locale = Locale(identifier: "en_US_POSIX")
    let date = formatter.date(from: sourceDateString)

    print(date)

    formatter.timeZone = NSTimeZone.local
    formatter.dateFormat = "MMM dd,yyyy hh:mm a"

    let strDate = formatter.string(from: date!)
    print(strDate)

I forgot to add "POSIX".

Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39
0

Quick fix for this is to convert 12hr to 24 hr format.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateformatter.dateFormat = @"hh:mm a";
NSDate *date = [dateformatter dateFromString:departTime];
dateformatter.dateFormat = @"HH:mm";
NSString *time24 = [dateformatter stringFromDate:date];
departTimelbl.text=time24;

here you can replace formatter.dateFormat = "MM/dd/yyyy hh:mm a" with formatter.dateFormat = "MM/dd/yyyy HH:mm

Adam Bardon
  • 3,829
  • 7
  • 38
  • 73
0

Code written in Swift 3.0

You need to give local identifier on date formatter.

Here is the following working and tested code.

let sourceDateString = "12/22/2016 09:19 AM"
    let formatter = DateFormatter()
    formatter.dateFormat = "MM/dd/yyyy hh:mm a"
    formatter.locale = Locale(identifier: "en-US")
    formatter.timeZone = TimeZone(identifier: "UTC")
    let date = formatter.date(from: sourceDateString)

    print("\(date)")

    formatter.timeZone = NSTimeZone.local
    formatter.dateFormat = "MMM dd,yyyy hh:mm a"

    let strDate = formatter.string(from: date!)
    print(strDate)

Please let me know if it works.

Nilesh
  • 533
  • 7
  • 19