While working on a PowerShell automation script, one of my team members recently received a requirement to calculate the date differences between two dates. This is an interesting topic. I will explain various methods to calculate date differences in PowerShell, including days, months, years, hours, minutes, and even milliseconds.
To calculate the difference between two dates in PowerShell, you can use the New-TimeSpan cmdlet. For example, if you have two dates $date1 and $date2, you can create a TimeSpan object with $date_diff = New-TimeSpan -Start $date1 -End $date2. This TimeSpan object will give you the difference in days, hours, minutes, and other units through its properties like Days, TotalHours, and TotalMinutes.
Calculate Differences Between Two Dates in PowerShell
To calculate the difference between two dates in PowerShell, the most common method is using the New-TimeSpan cmdlet. This cmdlet allows us to create a TimeSpan object representing the time interval between two dates.
Here is the basic syntax.
$date1 = Get-Date "2024-01-01"
$date2 = Get-Date "2024-09-18"
$date_diff = New-TimeSpan -Start $date1 -End $date2In this example, $date1 and $date2 are DateTime objects, and $date_diff is a TimeSpan object that contains the difference between the two dates.
I executed the above script using VS code, and you can see the output in the screenshot below:

Check out Add Days to a Date in PowerShell
Examples of Calculate Date Differences
Now, let me show you a few real examples of calculating the date difference between two dates in PowerShell.
Example 1: Difference in Days
To calculate the difference in days between two dates in PowerShell, you can simply access the Days property of the TimeSpan object. The Days property returns the number of whole days represented by the TimeSpan.
$date1 = Get-Date "2024-01-01"
$date2 = Get-Date "2024-09-17"
$date_diff = New-TimeSpan -Start $date1 -End $date2
$days_diff = $date_diff.Days
Write-Output "Difference in days: $days_diff"In this example, $date_diff.Days gives us the number of days between January 1, 2024, and September 17, 2024. This is particularly useful for tasks like calculating the number of days remaining until a deadline or the number of days an event has lasted.
You can see the output in the screenshot below:

Example 2: Difference in Months
Now, let me show you another example of calculating date differences in months between two dates in PowerShell.
Calculating the difference in months is a bit trickier since the TimeSpan object doesn’t directly provide this information. You can calculate it manually by considering the year and month properties of the DateTime objects.
Here is the complete script.
$date1 = Get-Date "2024-01-01"
$date2 = Get-Date "2024-09-17"
$years_diff = $date2.Year - $date1.Year
$months_diff = ($years_diff * 12) + ($date2.Month - $date1.Month)
Write-Output "Difference in months: $months_diff"Here, we first calculate the difference in years and then convert it to months. We add the difference in months from the same year to get the total number of months between the two dates. This method is useful when you need to calculate the duration of an event in months, such as the length of a project or subscription period.
Here is the exact output you can see in the screenshot below:

Read PowerShell Get-Date Minus 1 Day
Example 3: Difference in Years
To find the difference in years, you can subtract the year component of the two dates in PowerShell. This is the simplest calculation and is often used to determine age or the number of years between two significant events.
Below is an example of PowerShell date difference in years.
$date1 = Get-Date "2024-01-01"
$date2 = Get-Date "2024-09-17"
$years_diff = $date2.Year - $date1.Year
Write-Output "Difference in years: $years_diff"In this example, $years_diff gives us the number of years between the two dates. This can be particularly useful for calculating the number of years of service for an employee or the age of a person.
Here is the exact output in the screenshot below:

Example 4: Difference in Hours
Now, let me show you another example of calculating the date difference in hours in PowerShell.
For the difference in hours, you can use the TotalHours property of the TimeSpan object. The TotalHours property returns the total number of hours the TimeSpan represents, including fractional hours.
$date1 = Get-Date "2024-01-01"
$date2 = Get-Date "2024-09-17"
$date_diff = New-TimeSpan -Start $date1 -End $date2
$hours_diff = $date_diff.TotalHours
Write-Output "Difference in hours: $hours_diff"In this example, $date_diff.TotalHours gives us the total number of hours between the two dates. This is useful for scenarios where you need to calculate the duration of an event in hours, such as the total hours worked on a project or the duration of a flight.
You can see the exact output in the screenshot below:

Read Get Date and Time in 24-Hour Format in PowerShell
Example 5: Difference in Minutes
Similarly, to get the difference in minutes, use the TotalMinutes property. The TotalMinutes property returns the total number of minutes represented by the TimeSpan, including fractional minutes.
Below is the complete PowerShell script.
$date1 = Get-Date "2024-01-01"
$date2 = Get-Date "2024-09-17"
$date_diff = New-TimeSpan -Start $date1 -End $date2
$minutes_diff = $date_diff.TotalMinutes
Write-Output "Difference in minutes: $minutes_diff"In this example, $date_diff.TotalMinutes gives us the total number of minutes between the two dates. This can be particularly useful for calculating the duration of shorter events, such as meetings or phone calls.
I executed the above PowerShell script, and you can see the output in the screenshot below:

Read Get Date Without Time in PowerShell
Example 6: Difference in Milliseconds
Here is another example of “PowerShell date difference in milliseconds”.
For the difference in milliseconds, use the TotalMilliseconds property. The TotalMilliseconds property returns the total number of milliseconds represented by the TimeSpan, including fractional milliseconds.
$date1 = Get-Date "2024-01-01"
$date2 = Get-Date "2024-09-17"
$date_diff = New-TimeSpan -Start $date1 -End $date2
$milliseconds_diff = $date_diff.TotalMilliseconds
Write-Output "Difference in milliseconds: $milliseconds_diff"In this example, $date_diff.TotalMilliseconds gives us the total number of milliseconds between the two dates. This is useful for high-precision timing calculations, such as performance testing or measuring a system’s response time.
You can see the output in the screenshot below;

Conclusion
In this tutorial, I explained how to calculate date differences in PowerShell using the New-TimeSpan cmdlet. This cmdlet allows you to compute the interval between two dates, and by accessing the properties of the resulting TimeSpan object, you can quickly get the difference in days, months, years, hours, minutes, and milliseconds.
You may also like the following tutorials:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.