Use Powershell to Compare Dates Without Time | get-date

Working in Powershell with dates, time and time zones is critical if you have data or automated processes that reference different geographical areas. Understanding how to compare these dates with and without a time value can mean the difference between an incorrect report or failing processes.

We have written before about using Powershell and dates, you can review a shorter explanation called “Using Powershell to Compare the Difference Between Dates” where we work with get-date and assign it to a variable.

Let’s look at comparing dates in more depth and using different formats such as converting from a string to a date or datetime data type.

Using New-Timespan Commandlet

There is a new quick solution for returning values between dates, this is called the new-timespan commandlet. It will do much of the work defined below when looking for comparison values but it requires Powershell 5.1 minimum.

$mydate = New-TimeSpan -End (Get-Date -Date '11/23/2030')

$mydate.Hours

$mydate.Days

$mydate.Minutes

Date Format Conversion – Country

You should know that different countries use different formats for their date definition and you can see how each country looks at dates by visiting the Wikipedia page on date formats. We will be using the United States format in these examples, which are “Month Day Year” and “Year Month Day” (MDY, YMD) and try to express how to change to the format your project requires.

Maybe someday a smart person could create a standard and call it something like,.. ISO 8601 … that would define it once and for all. 🙂

Get-date & String to Date

We will start by getting and defining the date using the Get-Date commandlet and a simple string. Just by executing the get-date commandlet you will see that the result displayed is readable but not a usable format so we need to pull the values out or change it to remove the time and work with just the date.

get-date  ## Monday, November 25, 2030 7:21:02 AM

One method is to force the time to midnight so you are always dealing with the values at the day level without worrying about the time. Adding or subtracting days or months will be accurate as they are always at midnight.

(get-date).Date  ## Monday, November 25, 2030 12:00 AM

get-date -DisplayHint Date  ## time is still there, but not displayed

(get-date).AddDays(13)  ## add some days

(get-date).AddDays(-11)  ##  subtract days

Often we need to convert from a string to a date or datetime variable so Powershell can do its magic. Using “get-date -Date” we can pass a string and access the same methods as long as the date format is understood.

(get-date -Date '11/25/2030').AddDays(10)  ## add 10 days, at midnight

(get-date -Date '11/25/2030').AddDays(-30)  ## subtract 30 days, at midnight

## using a variable we can access these methods easily

$mydate = get-date -Date '11/25/2030'  ## assign to a variable

$mydate.AddDays(10)  
$mydate.AddDays(-30)  
$mydate.AddMonths(15)
$mydate.AddYears(-12)

Casting .Net Datetime

When dealing with strings we need to cast them into a format that Powershell can work with. In this post we are showing that you can pass the string to Get-Date and it becomes an object for Powershell to use. In our previous post we used the .NET [Datetime] cast to convert the string to a Datetime object so I wanted to show that example here too.

[datetime]'2030-3-21'  ## casting into a valid datetime string

get-date -Date '2030-3-21'  ## string is converted to the PS object

([Datetime]'2030-3-21').Date

(get-date -Date '2030-3-21').Date

Get-Date -Format String for File Naming

Since were dealing with strings, the -Format switch provides a string version of the date and time so it can be used in folder or file names without the slashes or spaces. Very useful.

get-date -Format FileDateTime  ## includes time 

get-date -Date '11/25/2030' -Format FileDateTime  ## midnight

get-date -Date '11/25/2030' -Format FileDateTimeUniversal  ## UTC Time

Compare Dates | Hours, Seconds and More

Now that we understand the formatting and string applications we can use Powershell to give is more information that we normally would have to calculate, such as the number of hours or seconds between dates. Lets add some days to NOW (get-date) and see what details Powershell makes available.
Notice the parenthesis…

((get-date).AddDays(12)) - (get-date)  ## difference between 12 days from today

You should see values like TotalHours and TotalMinutes. If we assign the result to a variable we can access these using the dot notation and easily pull the difference between the 2 dates in hours, seconds etc.

$mydate = ((get-date).AddDays(12)) - (get-date)

$mydate.TotalHours

$mydate.TotalMinutes

$mydate.TotalSeconds

Split Date and Time

This post is more about dealing with the Date and not the time but I wanted to add an example for splitting and retrieving the Date and Time separately. This is actually a String method and not part of the Get-Date commandlet.
In this example we are passing the date and time in as a string and splitting them into “Date” and “Time”.

$array = '11/21/2030 13:21:04'.Split()

$mydate = $array[0]
$mytime = $array[1]

$mydate
$mytime

We can then treat these array values as if they are just another string.

get-date -Date $mydate  ## time is midnight

[datetime] $mydate + $mytime  ## back to a Datetime string

## assign to a variable and use Get-Date 

$datetime = [datetime] $mydate + $mytime
get-date -Date $datetime

## since its a string we need to add a space for Get-Date

$combined = $mydate + " " + $mytime
get-date -Date $combined