0

I am using this library for a calendar: https://github.com/CVCalendar/CVCalendar

And I try to get the users current date and time, to display the correct date in calendar.

But when I try to do print("Date: \(Date())") I get this output:

2019-11-19 14:32:03 +0000

But if this was correct, it should be like this:

2019-11-19 15:32:03 +0000

(I live in Norway). So it give me 1 hour wrong.

Any tips on what to do?

Camile
  • 155
  • 3
  • 10
  • check this answer: https://stackoverflow.com/questions/46890038/swift-calendar-class-returning-wrong-date-for-weekday – Chris Nov 19 '19 at 14:42
  • You have to set your timezone.https://stackoverflow.com/questions/1699671/how-to-change-time-and-timezone-in-iphone-simulator – Thaier Alkhateeb Nov 19 '19 at 14:44

1 Answers1

1

From Apple's documentation:

A Date is independent of a particular calendar or time zone. To represent a Date to a user, you must interpret it in the context of a Calendar.

You need the help of Calendar:

let calendar = Calendar(identifier: .gregorian)
let currentDate = Date()
print(calendar.dateComponents(in: calendar.timeZone, from: currentDate))

the output will be:

calendar: gregorian (fixed) timeZone: Europe/Bratislava (current) era: 1 year: 2019 
month: 11 day: 19 hour: 15 minute: 47 second: 14 nanosecond: 285109996 weekday: 3 weekdayOrdinal: 3 quarter: 0 weekOfMonth: 4 weekOfYear: 47 yearForWeekOfYear: 2019 isLeapMonth: false 

So you could then access your calendar's components like hour, minute etc.

UPDATE:

@Camile answered in comments to their question using DateFormatter:

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

will give you

2019-11-19 16:38:03:+0100
Aleksey Potapov
  • 3,683
  • 5
  • 42
  • 65
  • How can I then store it in this format: `2019-11-19 15:32:03 +0000` ? – Camile Nov 19 '19 at 15:22
  • 1
    I tried this; https://pastebin.com/GH9BEeRx - And it seems to work. – Camile Nov 19 '19 at 15:33
  • @Camile check please that dateFormat string for timezone `dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss:Z"` – Aleksey Potapov Nov 19 '19 at 15:34
  • An extra; How can I convert the output back to a `Date()`? – Camile Nov 19 '19 at 16:01
  • @Camile `let formattedDate = dateFormatter.string(from:currentDate) let newDate = dateFormatter.date(from: formattedDate)` you will get again timeZone independent result: `2019-11-19 16:29:55 +0000`, see the first paragraph of the answer – Aleksey Potapov Nov 19 '19 at 16:30
  • Having some issues, please take a look: https://stackoverflow.com/questions/58938683/convert-from-string-to-date-with-current-timezone – Camile Nov 19 '19 at 17:16
  • @Camile `Date` is timezone independent. the answer from Leo is correct. https://stackoverflow.com/questions/28332946/how-do-i-get-the-current-date-in-short-format-in-swift/28347285#28347285 – Aleksey Potapov Nov 19 '19 at 17:32
  • Yea, I still don't get how to use it to save current time and date as Date(). – Camile Nov 19 '19 at 17:44