-1

I created an object that has a creation date property. The date property is calculated by using the Date() function, for getting the current date and time.

The date is correct but the time is 3 hours behind the clock of the simulator. How can I fix this?

Fayyouz
  • 682
  • 7
  • 18
  • 3
    Your code is fine. The correct time is being shown. There is nothing to fix. You just need to realize that when you print a `Date`, it shows the time in the UTC timezone. You must live 3 hours from UTC. – rmaddy Jul 29 '18 at 15:15

1 Answers1

2

The system displays the current date / time of UTC/GMT.

To get time/date for local time zone. use following code:

let currentDate = Date()
//7/29/18, 9:09 PM
print (DateFormatter.localizedString(
    from: currentDate,
    dateStyle: .short,
    timeStyle: .short))

//Sunday, July 29, 2018 at 9:09:27 PM India Standard Time
print (DateFormatter.localizedString(
    from: currentDate,
    dateStyle: .full,
    timeStyle: .full))

//Jul 29, 2018 at 9:09:27 PM
print (DateFormatter.localizedString(
    from: currentDate,
    dateStyle: .medium,
    timeStyle: .medium))

Above code is tested in India at 09:09 PM on 29th July 2018.

Rizwan
  • 3,324
  • 3
  • 17
  • 38