-3

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?

kiran
  • 4,285
  • 7
  • 53
  • 98

1 Answers1

1

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
}

Edit:

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?)

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Or using the pattern matching operator `startDate ... endDate ~= breakDate` – Leo Dabus Jan 29 '22 at 14:31
  • Btw if all date strings are in the same format and same timezone (UTC) no need to convert them to date – Leo Dabus Jan 29 '22 at 14:42
  • 1
    I personally dislike the pattern matching operator. I find it incredibly obtuse. In another thread it was determined that the pattern match operator and using inequalities generates exactly the same machine code, so use whichever form is easiest to understand. I submit that except for using it on a closed team that uses it a lot, the pattern match operator is going to be confusing and hard for other readers of the code to understand. – Duncan C Jan 29 '22 at 14:42
  • 1
    I think its readability is much better. As you said personal preference. – Leo Dabus Jan 29 '22 at 14:43
  • Btw it can also be written as `(startDate...endDate).contains(breakDate)` – Leo Dabus Jan 29 '22 at 14:45
  • If they are all in Internet date format and **guaranteed** to be in the same time zone then you can compare them as strings. If there is any chance that the dates might have a different time zone offset then that approach will break. If the date strings come from an external source then I would not assume they will all be in the same time zone. (And other string formats like the US mm/dd/yyyy date format won't allow string sorting of dates.) – Duncan C Jan 29 '22 at 14:47
  • I find the contains format much more intuitive than the pattern match syntax, and it follows DRY (Don't Repeat Yourself, for those that aren't familiar.) My proposed solution repeats the date being checked twice, which introduces the chance of future editing errors. I wonder what the assembler for the contains version is like with optimization turned on? The Clang compiler is so good I bet it's just as efficient as the others. – Duncan C Jan 29 '22 at 14:50