Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string to date without time in Swift 3?

I have try to convert the date like 2017-02-11 string to date but the result give me back something like this 2017-02-10 16:00:00 +0000 . I am using the following code below.

let localDate = dateFormatter.string(from: date) //2017-02-11

    let dateFormatter2 = DateFormatter()
    dateFormatter2.dateFormat = "yyyy-MM-dd"

    let selectedDate = dateFormatter2.date(from: localDate)
    print("Did Select \(selectedDate)") // 2017-02-10 16:00:00 +0000

1 Answers

It's true that there is no Date without a time element, but if you need something that you can use to compare whole days, you can get the first moment of a given Date using

Calendar.current.startOfDay(for: dateWithTime)
like image 76
Russell Avatar answered Dec 02 '25 03:12

Russell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!