# How to see if the given date is in between two dates?

I am trying to create a function with which I can see if the current date is in between two given dates?
For example, I have a record that has an effective date 01-01-2010 and an expiration date 11-30-2025, and I am trying to see if the 11-01-2015 is in between two dates or not? If so, the function returns me true else false.
Any suggestions?

You can also use the `time.Time.After`, `time.Time.Before`, and `time.Time.Equal` methods:

``````func TimeIsBetween(t, min, max time.Time) bool {
if min.After(max) {
min, max = max, min
}
return (t.Equal(min) || t.After(min)) && (t.Equal(max) || t.Before(max))
}
``````
4 Likes

Hi @skillian,

Would this code work with both clock times (wall clock and monotonic) ?

It looks like time comparisons use the monotonic clock if the times both contain monotonic times. You can use `time.Time.Round(0)` to remove the monotonic clock time.

Hi @skillian,

Correct me if I am wrong: in the code above I want to enforce that all comparisons only take into account wall clock. Thus, at the beginning of that function I could add:

``````t.stripMono()
min.stripMono()
max.stripMono()
``````

This is because wall clock is immune to changing the speed of time on the machine the code is running. I cannot find practical examples of the difference between wall clocks and monotonic clocks.

In my mind I believe wall clock is: “2020-01-01 05:30:23” and monotonic clock is 1424322134. Which is stupid as the time of both clocks can be represented in the same two formats. Is there any practical way to visualise the difference between monotonic and wall clocks ? Any example ?

The monotonic clock is meant to determine relative sequences of events. The wall clock can be adjusted to account for leap seconds (or any other reason). If you had code like this:

``````startTime := time.Now()
doSomething()
endTime := time.Now()
``````

Then `startTime.Before(endTime)` will always return `true`. If you do this:

``````startTime := time.Now().Round(0)
doSomething()
endTime := time.Now().Round(0)
``````

Now `startTime.Before(endTime)` could return `false` if there was a leap-second or other time change between where `startTime` and `endTime` are set.

If you’re creating your times with something other than `time.Now`, then the time.Time does not have a monotonic component and the time comparison would use the wall clock/calendar comparison. Either way, I believe that `TimeIsBetween` should behave correctly with both/either monotonic or wall clocks.

Also, when serializing a time.Time object, only the wall clock get serialized ? - is this sentence correct ?
and both wall clock and monotonic clock, are, in memory int32 values ? - am I correct here as well

It is one of the go subjects not so clear to me.

What do you mean by serializing? If you mean the String function, then no, the monotonic clock is also included: https://play.golang.org/p/Z35Vf8gIg4Z.

One of the best things about Go is when you find the documentation for a type (e.g. `time.Time`), you can click on the heading from the documentation, and it’ll take you to the source code.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.