Converting UTC to EST in Swift - ios

I have a string of text in my application that conveys a time and the date, such as let time = "2017-07-09T09:17:08+00:00". I want to take this string, which is in UTC, and convert it to a string that presents the time in EST. For my example, the resulting string would be 2017-07-09T05:17:08+00:00. How can this be done?
I've tried using
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
dateFormatter.timeZone = TimeZone.current
let date = dateFormatter.date(from:time!)!
but printing the date gives me the same time in UTC.

All you need to do is to use a DateFormatter to convert your Date object into a String. By default, the date will be formatted to local time.
// First, get a Date from the String
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from:time!)!
// Now, get a new string from the Date in the proper format for the user's locale
dateFormatter.dateFormat = nil
dateFormatter.dateStyle = .long // set as desired
dateFormatter.timeStyle = .medium // set as desired
let local = dateFormatter.string(from: date)
Note that there is no need to set any timezone for either set of code in this case.

Related

How to change the timeZone in SwiftDate?

I have displayed the list of timezones in my app. If user selects a particular timezones, I need to change the local timezone to the selected timezone by the user.
let region = Region(tz: timeZoneName.timeZone , cal: cal, loc: cal.locale!)
let date = Date().inRegion(region: region).absoluteDate
Here is the problem, the region is changed to the selected timezone but the date issuing the local timezone.
A Date contains no timezone. From apple's docs: A specific point in time, independent of any calendar or time zone.
The timezone comes into play as soon as you want to present a date to the user. And that's what a DateFormatter is for. As #AlexWoe89 already pointed out, it let's you convert a string, containing a date into a Date object, but also lets you convert a given date into a string representing the date in the time zone you set to the timeZone property of DateFormatter.
let date = Date()
var dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
dateFormatter.timeZone = TimeZone(identifier: "America/Los_Angeles")
let dateString1 = dateFormatter.string(from: date)
dateFormatter.timeZone = TimeZone(identifier: "Germany/Berlin")
let dateString2 = dateFormatter.string(from: date)
This will store 2017-10-23 04:27 in dateString1, while the same date leads to 2017-10-23 13:27 in dateString2.
You can use DateFormatter as a solution, try something like this:
let dateString = "<yourDateAsString>"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // => there are a lot of identifiers you can use
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.defaultDate = Date()
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm” // => your needed time format
let convertedDate = dateFormatter.date(from: dateString)

cast "1900-01-01T00:00:00" string value to date

I've watching trough stack overflow to find the answer and I can't find it I want to cast this string value "1900-01-01T00:00:00" to Date format, I was trying with some formats like those:
"yyyy-MM-dd HH:mm:ss"
"EEE, dd MMM yyyy hh:mm:ss +zzzz"
"YYYY-MM-dd HH:mm:ss.A"
"yyyy-MM-dd HH:mm:ss.S"
but anyone of those its working.
and I want the date format like this
"dd-mm-yyyy"
Hope you can help me!
Thanks.
It is a two step process, first converting 1900-01-01T00:00:00 (known as a RFC 3999 or ISO 8601 date, referred to the specifications that define this format) into a Date object, and then converting that Date object back to a string in the form of 01-01-1900:
To convert your string in the form of 1900-01-01T00:00:00 into a Date object, you can use ISO8601DateFormatter:
let formatter = ISO8601DateFormatter()
formatter.formatOptions.remove(.withTimeZone)
let date = formatter.date(from: string)!
That is equivalent to the following DateFormat, in which one has to manually set the locale to en_US_POSIX (because RFC 3999/ISO 8601 dates use a Gregorian calendar, regardless of what the device's default calendar type) and sets the timeZone to GMT/Zulu, because usually RFC 3999/ISO 8601 dates are representing GMT unless specified otherwise:
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
let date = formatter.date(from: string)!
For more information about the importance of timezones and locales in parsing RFC 3999 and ISO 8601 dates, see Apple's Technical Q&A 1480.
Then, to convert that Date object to a string into 01-01-1900 (day, month, and year), you'd use a format string of dd-MM-yyyy (note the uppercase MM for "month", to distinguish it from mm for "minute"):
let formatter2 = DateFormatter()
formatter2.dateFormat = "dd-MM-yyyy"
let string = formatter2.string(from: date)
Two observations regarding the dateFormat string:
If this string is for displaying to the user, you might use use dateStyle rather than dateFormat, e.g.:
formatter2.dateStyle = .short
While this will generate a slightly different format, e.g. dd/MM/yy, the virtue of this approach is that the string will be localized (e.g. UK users will see MM/dd/yyyy, their preferred way of seeing short dates).
It just depends upon the purpose of your dd-MM-yyyy format. If it's for internal purposes, go ahead and use dateFormat. But if it's for showing dates in your UI, use dateStyle instead, and enjoy the localization that DateFormatter does automatically for you. For more information, see "Working With User-Visible Representations of Dates and Times" section of the DateFormatter reference.
Note that in the absence of a timeZone specified for this second formatter, it assumes that while the ISO 8601 date was in GMT, that you want to see the date in your local timezone. For example, (1900-01-01T00:00:00 GMT was Dec 31, 1899 at 4pm in California). If you want to see the date string of the original ISO 8601 object, not corrected for timezones, you'd just set the timeZone of this second formatter to be GMT as well, e.g.
formatter2.timeZone = TimeZone(secondsFromGMT: 0)
As others have pointed out, you want to avoid unnecessarily re-instantiating DateFormatter objects. So you might put these formatters in properties that are instantiated only once, or use an extension:
extension DateFormatter {
static let customInputFormatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions.remove(.withTimeZone)
return formatter
}()
static let customOutputFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy"
formatter.timeZone = TimeZone(secondsFromGMT: 0) // if you want date in your local timezone, remove this line
return formatter
}()
}
And then:
let input = "1900-01-01T00:00:00"
let date = DateFormatter.customInputFormatter.date(from: input)!
let output = DateFormatter.customOutputFormatter.string(from: date)
print(output)
This is how I do custom date formatters:
extension DateFormatter {
static let inDateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
return dateFormatter
}()
static let outDateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-mm-yyyy"
return dateFormatter
}()
}
And then use it like:
if let date = DateFormatter.inDateFormatter.date(from: "1900-01-01T00:00:00") {
let newDateString = DateFormatter.outDateFormatter.string(from: date);
print(newDateString) //prints 01-00-1900
}
This avoids any potential performance issues and is clear at the point of use, while still being concise.
Use this extension I created, where you can pass the format as a parameter.
extension String
{
func toDate( dateFormat format : String) -> Date
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
if let date = dateFormatter.date(from: self)
{
return date
}
print("Invalid arguments ! Returning Current Date . ")
return Date()
}
}
"1900-01-01T00:00:00".toDate(dateFormat: "yyyy-MM-dd'T'HH:mm:ss") //Plyground call test

best way to convert date in English to French, nsdate, localization

My property dob of Patient object is String and currently storing 12-Jan-2017 and I want to convert it to French locale such as 12-Janv.-2017.
Below are my steps:
Converting 12-Jan-2017 into 1954-09-07 04:00:00 UTC
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale.init(localeIdentifier: "en_US_POSIX")
dateFormatter.dateFormat = "dd-MMM-yyyy"
let date = dateFormatter.dateFromString("12-Jan-2017") // now date is 1954-09-07 04:00:00 UTC
Next I have to set the locale of dateFormatter to fr
dateFormatter.locale = NSLocale.init(localeIdentifier: "fr")
let frenchDate = dateFormatter.stringFromDate(date!) // now it is 12-Janv.-2017
I dont think it is the best way to do a conversion. Are there any other efficient way for it. Any comments are welcomed.
(NS)DateFormatter has methods to convert a date to a string representation and vice versa. There is no (built-in) method to convert
from one date format directly to another format without intermediate
(NS)Date. So what you are doing is fine.
Some suggestions though:
Use optional binding instead of forced unwrapping to avoid a crash
at runtime if the input string is not in a valid format.
Use DateFormatter.dateFormat(fromTemplate:...) to get the
date format string appropriate for the chosen locale.
Then your code would be (in Swift 3):
let inputDate = "12-Jan-2017"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "dd-MMM-yyyy"
if let date = dateFormatter.date(from: inputDate) {
dateFormatter.locale = Locale(identifier: "fr")
dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "dd-MMM-yyyy", options: 0, locale: dateFormatter.locale)
let frenchDate = dateFormatter.string(from: date)
print(frenchDate) // 12 janv. 2017
} else {
print("invalid input date")
}

Converting string EST time to localized time

Assume I'm given an input string like "7:00". I know that the time is EST but I want to convert it to localized time (so for people in PST it says 4:00). I tried the following code, but I keep getting a 24-hour time from it and the complete date (e.g: Optional(2000-01-01 16:00:00 +0000)). I want just the hour:min time...what am I doing wrong?
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH:mm"
dateFormatter.timeZone = NSTimeZone.localTimeZone()
gameClock = "\(dateFormatter.dateFromString("7:30"))"
println(gameClock)
You need to specify the time zone for your date formatter that you use to process the server date. By default, the time zone of the date formatter is the default time zone of the device.
So, for your above code, you should do this:
let dateStringFromServer = "07:00"
let serverDateFormatter = NSDateFormatter()
serverDateFormatter.dateFormat = "HH:mm"
serverDateFormatter.timeZone = NSTimeZone(name: "EST")
let dateFromServer = serverDateFormatter.dateFromString(dateStringFromServer)
let localDateFormatter = NSDateFormatter()
serverDateFormatter.dateFormat = "HH:mm"
let localizedStringForThatDate = localDateFormatter.stringFromDate(dateFromServer!)
The dateFormatter.dateFromString("7:30") gives you an NSDate not an NSString. You have make use of another NSDateFormatter object to get the date in desired string format.
let dateFormatterForDisplay = NSDateFormatter();
dateFormatterForDisplay.dateFormat = "HH:mm"
dateFormatterForDisplay.locale = NSLocale(localeIdentifier: "en_US_POSIX")
(you need the last line in the above if you want to force 12 hour mode, the default is NSLocale.currentLocale() and can be 12 hr or 24 hr mode for different locales)
Then
let displayString = dateFormatterForDisplay.stringFromDate(gameClock)

Format String to HH:mm in Swift

My code:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = dateFormatter.dateFromString("2015-09-01 00-32-40")
Result: 2015-08-31 17:32:40
But I want get result like this: 17:32. How do I resolve it?
If you're trying to get an NSDate and not a string representation, printing the NSDate will always show the full date time. To just show the hours and minutes, you'll have to create a string representation using an "HH:mm" date format, ex:
// Your original code
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = dateFormatter.dateFromString("2015-09-01 00-32-40")
// To convert the date into an HH:mm format
dateFormatter.dateFormat = "HH:mm"
let dateString = dateFormatter.stringFromDate(date!)
println(dateString)
Not sure why you're looking to get a result of "17:32", but this will produce "00:32" as a result from the original "00-32" portion of the string.

Resources