How to determine date falls in between two dates value?
My Work time startDate = 2021-01-27 09:00:00 +0000
& EndDate = 2021-01-27 18:00:00 +0000
How to validating Break falls in between working time period?
How to determine date falls in between two dates value?
My Work time startDate = 2021-01-27 09:00:00 +0000
& EndDate = 2021-01-27 18:00:00 +0000
How to validating Break falls in between working time period?
If you have Date objects, they are Comparable and Equatable. You can write code like:
if breakDate >= startDate && breakDate <= endDate {
// breakDate is between startDate and endDate
}
As pointed out by LeoDabus in the comments, you could also use the pattern match operator (although I personally don't like it)
if startDate ... endDate ~= breakDate {
// breakDate is between startDate and endDate
}
or contains:
if (startDate...endDate).contains(breakDate) {
// breakDate is between startDate and endDate
}
If you have date strings, create a date formatter that converts your date strings to Date
objects, then use code like the above.
If the date strings are always in "Internet" date format (ISO 8601), and you are certain they will always be in the same time zone, you can also use string comparison and compare the date strings directly (using any of the above forms of comparison, except maybe contains?)