1

So i am trying to format a date(in string) and get the dat from it.

This is my code:

var cycleDate = "01.10.19"

func getDate(date: String) -> Date {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd-MM-yy"
    let date = dateFormatter.date(from:date)!

    return date
}

func getCycleDay(date: String) {

    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale.current
    dateFormatter.timeZone = NSTimeZone.local
    let myDate = getDate(date: date)
    let weekday = Calendar.current.component(.weekday, from: myDate)

    let stringwWeekday = dateFormatter.weekdaySymbols[weekday]
    print(date)
    print(myDate)
    print(weekday)
    print(stringwWekday)

}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    getCycleDay(date: cycleDate)

}

The prints are as follow:

01.10.19

2019-09-30 21:00:00 +0000

3

Wednesday

As you can see the date is not the correct one, I check that the time zone and locale are as needed.

What is the issue? Thanks in advance!

ironRoei
  • 2,049
  • 24
  • 45

0 Answers0