Last active
March 7, 2017 12:25
-
-
Save MattCheetham/c717648e9604d19615d7ee89dbc695aa to your computer and use it in GitHub Desktop.
Swift date formatting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
/// We're working with appointments. | |
/// So I get this string from the server and in some places I need to display it in the users local timezone and in other places I need to display it as the time and date that it was booked | |
var serverTimeString = "2017-03-21T19:00:00-07:00" | |
let localisedDateFormatter = DateFormatter() | |
localisedDateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ" | |
// Displays as March 22nd, 2am (GMT) - CORRECT | |
var date = localisedDateFormatter.date(from: serverTimeString) | |
// TO DO: Output date as March 21st, 7pm. | |
// ?? |
Could likely tidy it up. I've used regex and timezone calculations to try and catch any timezone not just -7 hours
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@MattCheetham
This works, I get Tuesday, 21 March 2017 at 19:00:00 GMT-07:00