Apr 28

Determine If A Date Is Between Two Dates

If you need to know if a date is between two dates, you can easily figure this out by treating the date as a number and doing comparisons.  This can be useful for instances where you need a script to do a different task on different months, days, years, etc.  Let’s start with our first example, which will demonstrate how the comparison works.

Example 1 – Time Matters

In this example, if the current date/time is 4/27/2015 12:01:00 PM then it would return False due to the time being outside of our defined end range.  If the date/time was 4/27/2015 12:00:00 PM then it would return True.  If you do not specify the time then it will automatically be defined as 12:00:00 AM in the $start and $end variables; it is important to understand this or else it will affect your date comparisons.  If you do not want time to be a factor, I’d suggest always defining your start time as 12:00:00 AM and your end time as 11:59:59 PM.

Example 2 – Year Does Not Matter

In this example, I’m replacing the year from any specified date and making it the current year.  This allows us to focus our comparison based on the month, day, and time.  For example, if the current date/time is 4/27/2015 12:00:00 PM and want to know if our script is running during April.  We can input the start date/time as 4/1/2016 12:00:00 AM our end date/time as 4/30/2016 11:59:59 PM and the script would return True despite the year not matching.


With a basic understanding of compairing dates in powershell, you can easily customize the above scripts to meet any custom needs.