String to date with UTC timezone - ios

I am struggling with Date and I'm assuming is TimeZone.
Currently I get from my backend a string like this "2020-04-07" and when I try to convert it to date it turns into 2020-04-06 22:00:00 +0000. I am in Spain (UTC+2) which I guess this is why it removes 2 hours?
This is my date formatter:
var dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = TimeZone.current
return dateFormatter
}()
And I call it dateFormatter.date(from: startDateString)
I am setting my current timezone but seems to be ignoring it or am I missing something?
I have followed a lot of answers from here but it's always the same result.
Thank you

The Date object does not have any inherent locale / time zone. It just represents a moment in time. If you want to see that Date as a string in a specific locale/time zone you have to use a date formatter. Or there's descriptionWithLocale. If you use print it will print a debug description of the Date instance in UTC.

Related

DateFormatter returns nil date for some users regardless of locale/isDaylightSavingTime/timezone

We use Disqus for our comments functionality. Its comments timestamp is in ISO8601 date format, e.g. "2019-12-11T01:45:23". We tried to parse that string with DateFormatter, set up like this:
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
return dateFormatter
It works well for most users. However, we receive reports for a small amount of users that the formatter returns nil. Our initial hypothesis for the cause are as follows.
the date format "yyyy-MM-dd'T'HH:mm:ss" was wrong
locale
timezone
daylight saving makes some time theoretically non-existent
calendar
or something else...
All of them are parameters we use to set up the DateFormatter. We tried many combinations, including ones we got from user reports. We tried the same date, locale, timezone, calendar, and the time itself as in user reports.
locale: en_GB, timezone: Europe/London, calendar: gregorian, isDaylightSavingTime: 1
locale: en_TR, timezone: Etc/GMT-3, calendar: gregorian, isDaylightSavingTime: 0
locale: es_MX, timezone: America/Mexico_City, calendar: gregorian, isDaylightSavingTime: 1
locale: en_ID, timezone: Asia/Jakarta, calendar: gregorian, isDaylightSavingTime: 0
But we could not reproduce.
Moreover, we did another test with a bunch of dates spreading throughout the year. And perform hidden parsing test on every user. It seems on device that returns nil date, it returns nil for EVERY date. The date string list looks like this...
[
"2019-01-11T01:45:23",
"2019-02-11T01:45:23",
"2019-03-11T01:45:23",
"2019-04-10T01:45:23",
"2019-04-11T01:45:23",
"2019-04-12T01:45:23",
"2019-05-11T01:45:23",
"2019-06-11T01:45:23",
"2019-07-11T01:45:23",
"2019-08-11T01:45:23",
"2019-09-11T01:45:23",
"2019-10-11T01:45:23",
"2019-11-11T01:45:23",
"2019-12-11T01:45:23",
])
We later found that there is another date formatter called ISO8601DateFormatter. It seems to be more appropriate for this parsing. Here is the how we set it up.
let dateFormatter = ISO8601DateFormatter()
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.formatOptions = [.withFullDate, .withTime, .withDashSeparatorInDate, .withColonSeparatorInTime]
return dateFormatter
With this ISO8601DateFormatter, the issue is fixed.
But I still want to know what can cause DateFormatter to fail on some device? Is there other factors than locale/isDaylightSavingTime/timezone that I'm not aware of?
Is it the user's setting for 12-hour vs. 24-hour clock?
iOS can change a date formatter's format string to match the user's settings. The locale en_US_POSIX is recommended for fixed formats to prevent format changes due to user settings.
Please use ISO8601 date format with timezone
"2019-12-11T01:45:23.000Z"
Format:
"XXXX-XX-XX" = year, month, day,
"T" = separator
"XX:XX:XX.XXX" = hour, minute, seconds, milliseconds
"Z" = timezone designator for zero offset, a.k.a. UTC, GMT, Zulu time
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
return dateFormatter
For anybody still facing this issue, what worked for me was to set dateFormatter.isLenient = true as it is described here: DateFormatter returning nil for a valid Date string

String did not Convert Properly to Date in ios Swift

I want to convert string to date everything is fine but still, it gives me an incorrect date according to string.
Code:
import UIKit
public class Utils {
public class func converServerTimeStampToDate (_ timeStamp: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy, hh:mm:ss a"
return dateFormatter.date(from: timeStamp)!
}
}
print(Utils.converServerTimeStampToDate("12/06/2017, 06:48:03 am"
))
-----OutPut-----
2017-12-06 14:48:03 +0000
To solve this problem set timezone
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
By default it is taking current time zone that's why your output is different from input.
It is correct result. When you convert any string into Date it will convert it in UTC timezone. And when you convert that date into string again it will be in your current timezone. So, convert that date into string and you real date you will get. So, your Date object always be in UTC timezone and your string will be always in your local timezone. And when you displays your date in label or any other control then definitely you will display in string format and it will be in your local timezone. So, there is nothing wrong in it. You just required to understand the concept!

How to save datetime with timezone in iOS?

I am new to iOS development, being an Android developer I am used to use have an object that saves a datetime with a given timezone (from Joda-Time library).
After reading the iOS documentation about dates and times (https://developer.apple.com/documentation/foundation/dates_and_times) I still have doubts about which class should I use to save datetimes. Given the Date/NSDate class description "A specific point in time, independent of any calendar or time zone." it seems very useless because it is timezone independent and time without a timezone does not make any sense, since it does not have any context.
My real problem (TL;DR):
I have a database where date times are stored in UTC like this "yyyy-MM-dd hh:mm:ss". I would like to init an object with some kind of DateFormatter (string with this format "yyyy-MM-dd hh:mm:ss") plus a timezone (UTC) to easily convert to any Timezone that I want (to show to the user on his default timezone time). How can I accomplish this in iOS?
Edit: Imagine I have a class Event with a title and a start time. Title is a String, what start time should be?
You use a DateFormatter for this.
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
formatter.timeZone = TimeZone(secondsFromGMT: 0)
The formatter.locale sets the current locale for the user and formatter.dateFormat sets the desired date format. In your case yyyy-MM-dd HH:mm:ss.
To call it simply:
let utcDateFromServer = "2017-01-01 22:10:10"
let date = formatter.date(from: utcDateFromServer)
A Date is a point in time, as mentioned in other comments & in the documentation.
If you want to convert the UTC time into local time, you'll need to first convert the String "yyyy-MM-dd hh:mm:ss" from your database into a Date using DateFormatter.
let dateStringUTC = "2018-01-01 00:00:00"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
//Set the input timezone (if you don't set anything, the default is user's local time)
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let date : Date = dateFormatter.date(from: dateStringUTC)!
Then convert the Date back into String using DateFormatter with the respective TimeZone
let outputDateFormatter = DateFormatter()
outputDateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
//Set the output timezone (if you don't set anything, the default is user's local time)
//outputDateFormatter.timeZone = someTimeZone
let dateString = outputDateFormatter.string(from: date)
print(dateString)
Output: 2017-12-31 17:00:00
And you can just change the input and output timezone to do the opposite.

Convert 12 hour date format into 24 hour format & vice versa in swift?

I have a date from a UIControl. I get date as string such as 06-12-2016 01:25 PM. Now I want to convert it into a format as 2016-12-06 13:25:00 I have tried below code t do so but it just gives me wrong date for ex. 2016-12-06 18:55:00. I have used below code for this:
func converStingToDate(str_date:String)->String
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy H:mm a"
let date = dateFormatter.date(from: str_date)
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return dateFormatter.string(from: date!)
}
Please suggest me a better code. Also if you guys can help me to understand all this date formatting concept how it is done ?
The code is correct and the date is correct (although you should use h for 12-hour hour).
Your time zone is UTC+0530 and print() displays the date in UTC.
If you need to print the correct local time set the time zone of the formatter to UTC:
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")`

Convert UTC NSDate into Local NSDate not working

I have a string getting from server as "yyyy-MM-dd'T'HH:mm:ss.SSS'Z"
I convert this string into NSDate by this formate.
class func convertUTCDateToLocateDate(dateStr:String) -> NSDate{
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
dateFormatter.timeZone = NSTimeZone(name: "UTC")
let date = dateFormatter.dateFromString(dateStr)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z"
dateFormatter.timeZone = NSTimeZone.localTimeZone()
let timeStamp = dateFormatter.stringFromDate(date!)
let dateForm = NSDateFormatter()
dateForm.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
dateForm.timeZone = NSTimeZone.localTimeZone()
let dateObj = dateForm.dateFromString(timeStamp)
return dateObj!
}
Suppose the parameter string is "2016-11-05T12:00:00.000Z" but when i convert this string and return a NSDate object it doesn't change the time according to my local time. I get my correct time in the timeStamp string (in above code). But when i try to convert that timeStamp string into NSDate it again shows that date and time which i got as a parameter.
You shouldnt change a NSDate's time. NSDates are just a point in time, counted by seconds. They have no clue about timezones, days, month, years, hours, minutes, seconds,… If printed directly they will always output the time in UTC.
If you change the date to show you the time of your timezone you are actually altering the time in UTC — hence your date becomes representing another point in time, no matter of the timezone.
Keep them intact by not altering them, instead when you need to display them do it via a date formatter.
If you need to do time calculations that are independent of timezones you also can work with NSDateComponents instead.
NSDate doesn't have a timezone. It's a point in time, independent of anything, especially timezones. You cannot "convert a UTC NSDate to a local NSDate", the statement itself doesn't make any sense.

Resources