0

Please check the code bellow:

func testDate() {
        let calendar = Calendar.current
        let dateEnd = calendar.date(byAdding: .day, value: 15, to: Date())

        let df:DateFormatter = DateFormatter()
        df.dateFormat = "MMMM dd, YYYY HH:mm"
        df.timeZone = calendar.timeZone
        df.timeZone = Calendar.current.timeZone

        let startDateLabel = df.string(from: dateEnd!)
        print(startDateLabel)

        let min = df.date(from: startDateLabel)!
        print(min)
    }

Output

print1: March 12, 2020 14:48

print2: 2019-12-22 09:48:00 +0000

Nirav Kotecha
  • 2,493
  • 1
  • 12
  • 26
Muneeb Awan
  • 115
  • 1
  • 8

2 Answers2

1

Use below dateFormat instead

df.dateFormat = "MMMM dd, yyyy HH:mm"
PGDev
  • 23,751
  • 6
  • 34
  • 88
-1

printing the date to console would give you the print2. It's printing the Date formats debug description. If you require a particular format you need to convert date into string with a DateFormatter. Check this link you need more help to get the desired date format.

Frankenstein
  • 15,732
  • 4
  • 22
  • 47
  • hey @Frankenstein that is not the problem .. the issue is that print 1 is showing March 12 2020 .. when it is converted back to "Date" it changes to December 22 2019! – Muneeb Awan Feb 26 '20 at 10:02
  • Check the link given in the above answer if you have more doubt regarding how to create a date format. – Frankenstein Feb 26 '20 at 10:05