Skip to content

Instantly share code, notes, and snippets.

@MattCheetham
Last active March 7, 2017 12:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattCheetham/c717648e9604d19615d7ee89dbc695aa to your computer and use it in GitHub Desktop.
Save MattCheetham/c717648e9604d19615d7ee89dbc695aa to your computer and use it in GitHub Desktop.
Swift date formatting
//: 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.
// ??
@tmdvs
Copy link

tmdvs commented Mar 7, 2017

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