0

I am using these lines of code to convert Date and time, to the current time zone:

let calendar = Calendar(identifier: .gregorian)
let currentDate = Date()
let dateFormatter = DateFormatter()

dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss:Z"
dateFormatter.timeZone = calendar.timeZone
dateFormatter.string(from:currentDate)

print(dateFormatter.string(from: currentDate))

The print output gives me:

2019-11-19 17:22:55:+0100

I am going to store the date in Realm, so how can I convert this back to a Date()?

Edit; I tried converting it, but it doesn't work:

        let calendar = Calendar(identifier: .gregorian)
        let currentDate = Date()
        let dateFormatter = DateFormatter()

        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss:Z"
        dateFormatter.timeZone = calendar.timeZone

        let dateString = dateFormatter.string(from:currentDate)
        let finalDate = dateFormatter.date(from: dateString)
        print(dateString) -> Gives me output: 2019-11-19 18:09:05:+0100 - This is the correct time!
        print(finalDate!) -> Gives me output: 2019-11-19 17:09:05 +0000

So the finalDate should store the current time as a Date(), but it doesn't get the correct time.

Camile
  • 155
  • 3
  • 10
  • If your goal is only storing a date and converting back you should use ISO8601 UTC time https://stackoverflow.com/questions/28016578/how-to-create-a-date-time-stamp-and-format-as-iso-8601-rfc-3339-utc-time-zone/28016692 – Leo Dabus Nov 19 '19 at 16:35
  • @LeoDabus Why is that? – Camile Nov 19 '19 at 16:59
  • Do you need the user timezone? Otherwise just use ISO8601. Btw if you don't set your date formatter locale to en_US_POSIX your parsing might fail – Leo Dabus Nov 19 '19 at 17:08
  • Did you check the link in the comment above? Btw when displaying a date to the user you can use this https://stackoverflow.com/questions/28332946/how-do-i-get-the-current-date-in-short-format-in-swift/28347285?r=SearchResults&s=1|22.3209#28347285 – Leo Dabus Nov 19 '19 at 17:17
  • @LeoDabus I am creating a workout journal app, therefor I would like to store the date and time that the user saved the workout. – Camile Nov 19 '19 at 17:20
  • Just use the link I posted – Leo Dabus Nov 19 '19 at 17:21
  • ISO8601 when storing it and use the second one to display the date – Leo Dabus Nov 19 '19 at 17:23
  • It will be localized you just need to choose short or long format – Leo Dabus Nov 19 '19 at 17:24
  • Not sure if it's because I've been up for over 24hours now without sleep, but I really don't understand it. Can you provide code for me please in an answer? – Camile Nov 19 '19 at 17:33
  • All the code you need is posted there already – Leo Dabus Nov 19 '19 at 17:34
  • I dont understrand how to use it and store like `yyyy-MM-dd HH:mm:ss:Z` as `Date()`. – Camile Nov 19 '19 at 17:35
  • you have to store it as string or number. But again use ISO8601 format – Leo Dabus Nov 19 '19 at 17:49
  • if you just need to convert from `Date` to `Data` and back to `Date` check this https://stackoverflow.com/a/47502712/2303865 – Leo Dabus Nov 19 '19 at 17:51
  • That is the correct time. 18:09:05+0100 is exactly the same point in time as 17:09:05+0000. Date represents a *point in time*. It has nothing to do with time zones. Time zones are printed in the debug output just for programmer convenience. – Rob Napier Nov 19 '19 at 18:08
  • If you want to display a Date (a point in time) in some human-readable form, for example including time zones, then you need to use a DateFormatter to format it into a string (as you're doing). `print(date)` doesn't demonstrate anything about time zones; Dates don't have time zones. – Rob Napier Nov 19 '19 at 18:11
  • @LeoDabus This gave me correct time; https://www.agnosticdev.com/content/how-convert-swift-dates-timezone - Is that any different then using ISO8601? – Camile Nov 19 '19 at 21:56
  • That’s ISO8601 but without fractional seconds – Leo Dabus Nov 19 '19 at 22:25

2 Answers2

0

DateFormatter converts in both directions. Using the same formatter, to convert it back, use .date(from:)

let date = dateFormatter.date(from: string)

Based on your comments, you may be misunderstanding what a Date is. A Date is nothing more and nothing less than a number of seconds since the reference time. It doesn't know anything about time zones. It doesn't know anything about calendars. It doesn't even know anything about minutes or hours. It is only a number of seconds since the reference time (it is literally a small wrapper around a single Double value).

When you print a Date, it calls .description, and as a programmer convenience that is generated by a default DateFormatter that turns it into a human readable string purely for debugging purposes. That string does not imply that the Date has calendar or a time zone attached to it. It's just a programmer convenience. It should never be used by the program itself.

If you want to deal with time zones, you must use a Calendar to compute various DateComponents, or a DateFormatter (which uses a Calendar) to generate a String.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
0

DateFormatter has an option to convert string to date as well:

let calendar = Calendar(identifier: .gregorian)
let currentDate = Date()
let dateFormatter = DateFormatter()

dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss:Z"
dateFormatter.timeZone = calendar.timeZone
dateFormatter.date(from: yourStringDate)
Marina Aguilar
  • 1,151
  • 9
  • 26