3

i want to get this type of formate -> April 20, 2018 at 9:02:00 PM UTC+5:30 for firestore timestamp. i already try this code but do not get proper formate as above.

let date = Date()
let formatter = DateFormatter()

formatter.dateFormat = "MMMM d yyyy hh:mm:ss a Z"
formatter.timeZone = NSTimeZone(name: "IST") as TimeZone?

let result = formatter.string(from: date)
print(result)

already use this formatter formatter.dateFormat = "MMMM d yyyy'T'hh:mm:ss a Z"

Thanks

5
  • "already try this code but do not get proper formate as above." What did it give you? For instance, I don't see a 'at' in your format. Commented Apr 23, 2018 at 9:20
  • Need to add "at" in between are you sure.? Commented Apr 23, 2018 at 9:20
  • yes @AbhirajsinhThakore Commented Apr 23, 2018 at 9:21
  • 1
    @Rock One suggestion, save timeStamp in unix format like this1524476327 and parse it at client side for UI purpose. Commented Apr 23, 2018 at 9:39
  • Don't know why new question for each dateFormat there are many formats so whenever requirement change you will ask a new question. Why not just google for that particular format? Commented Apr 23, 2018 at 9:47

3 Answers 3

8

You can use like this:

    let date = Date()
    let formatter = DateFormatter()
    formatter.dateFormat = "MMMM dd, yyyy 'at' hh:mm:ss a 'UTC'Z" //If you dont want static "UTC" you can go for ZZZZ instead of 'UTC'Z.
    formatter.timeZone = TimeZone(abbreviation: "IST")
    let result1 = formatter.string(from: date)
    print(result1)

Note: If you do not want the UTC to be static in the string, you can also use the format "MMMM dd, yyyy 'at' hh:mm:ss a ZZZZ"

Thank you Larme For the format improvement

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

its possible to UTC+5:30 instead of +0530 ?
Please check the Edited answer
formatter.dateFormat = "MMMM d, yyyy 'at' HH:mm:ss a Z" does the same job without needing to combine.
HH instead of hh is strange with a AM/PM afterwards.
Thanks @Larme for the suggestion,
|
4

do like

    let formatter = DateFormatter()
    //If you want it in your specific format you can simply add 'UTC'
    formatter.dateFormat = "MMMM dd, yyyy 'at' hh:mm:ss a 'UTC'Z"
    formatter.timeZone = NSTimeZone(name: "IST") as TimeZone?
    let result = formatter.string(from: Date())
    print(result)

Note: if you want to set the currentTimezone for your string then use the format "MMMM dd, yyyy 'at' hh:mm:ss a ZZZZ"

you get the output as

enter image description here

Comments

2

Try this if you don't want leading zero in hour, when hour has single digit. Also you don't need to typecast NSTimeZone into TimeZone. You can directly use TimeZone.

let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "MMMM dd, yyyy 'at' h:mm:ss a 'UTC'Z"
formatter.timeZone = TimeZone(abbreviation: "IST")
let result = formatter.string(from: date)
print(result)

April 23, 2018 at 3:09:01 PM UTC+0530

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.