How can I convert string
like (2019-11-02
) without time
to date format
and get current Date
device without time
then compare
with together?
Asked
Active
Viewed 224 times
-7

jo jo
- 1,758
- 3
- 20
- 34
-
Not quite clear what your asking but if your string date is yyyy-MM-dd then you can convert current date to the same format and compare the strings. – Joakim Danielson Nov 12 '19 at 09:36
2 Answers
-1
As well as the above using string representations of date, you can actually work with just the dates themselves. Just converting a the string will give you a "start of day" date. The Calendar has a method which will do the same with a date, allowing you to compare the converted string to 'today'
func isDateToday(dateString: String) -> Bool {
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd"
let date = df.date(from: dateString)
let today = Calendar.current.startOfDay(for: Date())
return date == today
}

flanker
- 3,840
- 1
- 12
- 20
-
@LeoDabus agreed in general, but not necessary as it's not specifying 12am - it's using the current calendar to establish what start of day is. This takes into account things like locale, daylight saving etc. This is generally a better approach as even noon is subject to influences of daylight saving. – flanker Nov 12 '19 at 11:32
-
I get different outputs from your code than your comments, but still not sure what your point is anyway. As both dates are using the current calendar (DateFormatter defaults to it and it's specified for the other) they'll both be treated the same w.r.t. start of day. If you think you have a better answer please post it so we can all benefit from your knowledge. – flanker Nov 12 '19 at 14:04
-
I will thanks :-) as parsing works fine on first daylight saving date. Just tested it again. If Calendar couldn't cope with daylight saving it wouldn't be much use. – flanker Nov 12 '19 at 14:22
-
-
Now that's weird - exactly same code: https://drive.google.com/open?id=1INMk-lmwGNbULQbGqdYLH2Zv8Mo3YUXV and this years daylight saving change date, (so ensuring correct locale etc): https://drive.google.com/open?id=1-t8zQJCM33q3B49PqOnUWDX5JKmhvjDM – flanker Nov 12 '19 at 15:06
-
This is because if you don’t set the locale date formatter will be affected by the user device locale and settings. You should set the calendar as well if you there is no time in your date string – Leo Dabus Nov 12 '19 at 15:55
-
Already tried that and made no difference (including aligning locale region code & DST date for other countries) and still worked. I'd expect this anyway, as default Calendar is Gregorian and, as you say, default locale is current locale. The whole purpose of using Calendar is that it is designed to mitigate all location/timezone weirdness. Gonna have to park this until I've got time to get into it in depth, but if nothing else all this should encourage others to check out the edge cases :-) – flanker Nov 12 '19 at 16:27
-2
You can convert the string to date using a dateFormatter then compare with the current date using an if statement
import Foundation
//convert string to date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let myDate = dateFormatter.date(from: "2019-11-02")
//convert today's date in the same formate
let currentFormatter = DateFormatter()
currentFormatter.dateStyle = .short
currentFormatter.dateFormat = "yyyy-MM-dd"
let today = currentFormatter.string(from: Date())
let todayDate = dateFormatter.date(from: today)
//Compare the two date's
if myDate == todayDate {
print("ok")
}

byaruhaf
- 4,128
- 2
- 32
- 50
-
-
it's optional because the date conversion may fail. you can unwrap the optional with if let or if you are very sure then force unwrap it with dateFormatter.date(from: "2019-11-02")! – byaruhaf Nov 12 '19 at 10:37