How to Compare Dates Without Time in PowerShell?

Recently, one of my team members was working with dates in PowerShell and needed to compare them without considering the time component. In this tutorial, I will explain different methods for comparing dates in PowerShell without the time component.

To compare dates without considering the time in PowerShell, you can use the Get-Date cmdlet combined with the ToString("yyyy-MM-dd") method. This approach formats the dates to a string representation that includes only the year, month, and day, effectively stripping off the time component. For example, you can compare two dates as follows:

$date1 = (Get-Date).ToString("yyyy-MM-dd")
$date2 = (Get-Date "2024-09-18").ToString("yyyy-MM-dd")

if ($date1 -eq $date2) {
    Write-Output "The dates are equal."
} else {
    Write-Output "The dates are not equal."
}

This method ensures that only the date parts are compared, ignoring any time differences.

PowerShell: Compare Dates Without Time

In many scenarios, you might need to compare dates without time to perform tasks like filtering records, scheduling tasks, or validating data. There are various methods in PowerShell to compare dates without time.

Method 1: Using Get-Date and ToString()

The best way to compare dates without the time in PowerShell is to convert them to a string format that only includes the date part.

Syntax

$date1 = (Get-Date).ToString("yyyy-MM-dd")
$date2 = (Get-Date "2024-09-18").ToString("yyyy-MM-dd")

if ($date1 -eq $date2) {
    Write-Output "The dates are equal."
} else {
    Write-Output "The dates are not equal."
}

In this method, we use the Get-Date cmdlet to retrieve the current date and format it using the ToString("yyyy-MM-dd") method. This strips off the time component, leaving only the date.

Let me show you an example.

Example

$date1 = (Get-Date).ToString("yyyy-MM-dd")
$date2 = (Get-Date "2024-09-18").ToString("yyyy-MM-dd")

if ($date1 -eq $date2) {
    Write-Output "The dates are equal."
} else {
    Write-Output "The dates are not equal."
}

You can see the exact output in the screenshot below:

powershell compare dates without time

Check out Check If Date Is Older Than 30 Days in PowerShell

Method 2: Using DateTime Objects with AddDays()

Another approach to compare dates without time is to create DateTime objects and compare their dates by setting the time to midnight.

Syntax

$date1 = Get-Date
$date2 = Get-Date "2024-09-18"

if ($date1.Date -eq $date2.Date) {
    Write-Output "The dates are equal."
} else {
    Write-Output "The dates are not equal."
}

Here, we use the Date property of DateTime objects, which returns the date part with the time set to midnight. This property allows for direct comparison of dates without time.

Example

Let me show you an example.

$date1 = Get-Date
$date2 = Get-Date "2024-09-18"

if ($date1.Date -eq $date2.Date) {
    Write-Output "The dates are equal."
} else {
    Write-Output "The dates are not equal."
}

You can see the output in the screenshot below:

PowerShell how to Compare Dates Without Time

Read Get Date Differences Between Two Dates in PowerShell

Method 3: Using Custom Date Formatting

You can also format the dates as strings in a custom format and then compare them. We can use custom date formatting in PowerShell to compare dates without time.

Syntax

$date1 = (Get-Date).ToString("yyyy-MM-dd")
$date2 = (Get-Date "2024-09-18").ToString("yyyy-MM-dd")

if ($date1 -eq $date2) {
    Write-Output "The dates are equal."
} else {
    Write-Output "The dates are not equal."
}

This method utilizes the ToString method with a custom format to strip off the time component, making it easy to compare just the dates.

Example

Here is an example.

$date1 = (Get-Date).ToString("yyyy-MM-dd")
$date2 = (Get-Date "2024-09-18").ToString("yyyy-MM-dd")

if ($date1 -eq $date2) {
    Write-Output "The dates are equal."
} else {
    Write-Output "The dates are not equal."
}

You can see the output in the screenshot below:

compare dates without time powershell

Read Add Days to a Date in PowerShell

Method 4: Using New-TimeSpan

You can also use New-TimeSpan to compare dates without considering the time in PowerShell.

Syntax

$date1 = Get-Date
$date2 = Get-Date "2024-09-18"

$timespan = New-TimeSpan -Start $date1.Date -End $date2.Date

if ($timespan.Days -eq 0) {
    Write-Output "The dates are equal."
} else {
    Write-Output "The dates are not equal."
}

The New-TimeSpan cmdlet calculates the difference between two dates. By comparing the Days property, you can determine if the dates are the same without considering the time.

Example

Let me show you an example.

$date1 = Get-Date
$date2 = Get-Date "2024-09-18"

$timespan = New-TimeSpan -Start $date1.Date -End $date2.Date

if ($timespan.Days -eq 0) {
    Write-Output "The dates are equal."
} else {
    Write-Output "The dates are not equal."
}

I executed the above PowerShell script using VS code, and you can see the exact output in the screenshot below:

How to Compare Dates Without Time in PowerShell

Conclusion

In this tutorial, I explained how to compare dates without the time component in PowerShell in multiple ways. With examples, I have explained how to use the ToString method, the Date property of DateTime objects, custom date formatting, or the New-TimeSpan cmdlet, etc. If you still have questions, leave a comment below.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.