PowerShell Get Difference Between Two Dates in Minutes

When working with PowerShell, I recently got a requirement to calculate the difference between two dates in minutes. In this tutorial, you’ll learn — step by step — how to get the difference between two dates in minutes using PowerShell. I will show you from the basics to advanced use cases with examples.

Date and Time in PowerShell

PowerShell represents dates and times using the .NET DateTime object. This object provides a wealth of properties and methods for manipulating and comparing dates.

You can create a DateTime object in several ways:

# Get the current date and time
$now = Get-Date

# Create a specific date and time
$customDate = Get-Date "2025-11-13 08:30:00"

When you display $now, PowerShell will output something like:

Thursday, November 13, 2025 3:45:00 PM

This object can be used for a variety of operations — including calculating the difference between two dates.

Check out PowerShell (Get-Date).AddDays(0)

Using Get-Date to Work with DateTime Objects

The Get-Date cmdlet is the foundation for date manipulation in PowerShell. You can use it to retrieve the current time or specify custom dates.

For example:

$startDate = Get-Date "2025-11-13 08:00:00"
$endDate   = Get-Date "2025-11-13 10:30:00"

Now, we have two DateTime objects. The next step is to calculate the difference between them.

Calculating Time Difference with New-TimeSpan

PowerShell provides a built-in cmdlet called New-TimeSpan, which is specifically designed to calculate the difference between two dates.

Basic Syntax

New-TimeSpan -Start <DateTime> -End <DateTime>

Example

Here is an example.

$startDate = Get-Date "2025-11-14 08:00:00"
$endDate   = Get-Date "2025-11-14 10:30:00"

$timeDifference = New-TimeSpan -Start $startDate -End $endDate
$timeDifference

Output:

Days              : 0
Hours             : 2
Minutes           : 30
Seconds           : 0
Milliseconds      : 0
Ticks             : 90000000000
TotalDays         : 0.104166666666667
TotalHours        : 2.5
TotalMinutes      : 150
TotalSeconds      : 9000
TotalMilliseconds : 9000000

As you can see, New-TimeSpan returns a TimeSpan object, which contains multiple properties like TotalMinutes, TotalHours, and TotalSeconds.

Here is the exact output in the screenshot below:

powershell get difference between two dates in minutes

Check out How to Find Files Modified After a Specific Date Using PowerShell?

Extracting the Difference in Minutes

To get the exact difference in minutes, you simply need to access the TotalMinutes property of the TimeSpan object.

$startDate = Get-Date "2025-11-14 08:00:00"
$endDate   = Get-Date "2025-11-14 10:30:00"

$timeDifference = New-TimeSpan -Start $startDate -End $endDate
$minutesDifference = $timeDifference.TotalMinutes
Write-Output "The difference is $minutesDifference minutes."

Output:

The difference is 150 minutes.

Here is the exact output in the screenshot below:

get difference between two dates in minutes in powershell

Tip:

  • TotalMinutes returns a floating-point number (e.g., 150.5).
  • If you need only whole minutes, you can round or cast it to an integer:
[int]$minutesDifference = [math]::Round($timeDifference.TotalMinutes)

Check out How to Find Files Modified After a Specific Date Using PowerShell

Alternative Method: Subtracting DateTime Objects Directly

Another powerful feature of PowerShell is that you can subtract DateTime objects directly. The result is automatically a TimeSpan.

Example:

Here is an example.

$startDate = Get-Date "2025-11-13 08:00:00"
$endDate   = Get-Date "2025-11-13 10:30:00"

$timeDifference = $endDate - $startDate
$timeDifference.TotalMinutes

Output:

150

This method is clean, efficient, and widely used in scripts where performance and simplicity matter.

Read How to Concatenate String and DateTime in PowerShell

Formatting and Rounding Minute Differences

Sometimes you’ll want to present the result in a user-friendly format. For example, when generating reports or logs.

Example: Display with Formatting

Here is an example of displaying the time difference in minutes with formatting.

$minutes = [math]::Round($timeDifference.TotalMinutes, 2)
Write-Host ("The time difference is {0} minutes." -f $minutes)

Output:

The time difference is 150 minutes.

You can also format the output as part of longer strings or convert it into hours and minutes:

$hours = [math]::Floor($timeDifference.TotalHours)
$remainingMinutes = $timeDifference.Minutes
Write-Host "$hours hours and $remainingMinutes minutes difference."

Output:

2 hours and 30 minutes difference.

Check out Create a Folder with Today’s Date and Copy Files to it using PowerShell

Practical Examples and Use Cases

Now, let me show you some practical examples of real-time use cases.

1. Calculating Script Execution Time

You can measure how long a script takes to run:

$start = Get-Date
Start-Sleep -Seconds 90  # Simulate a task
$end = Get-Date

$duration = $end - $start
Write-Host "Script execution time: $($duration.TotalMinutes) minutes"

Output:

Script execution time: 1.5 minutes

2. Monitoring System Uptime

If you have a timestamp of the last system reboot, you can calculate uptime:

$lastBoot = (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
$now = Get-Date
$uptime = $now - $lastBoot

Write-Host "System uptime: $([math]::Round($uptime.TotalMinutes)) minutes"

3. Comparing Log File Timestamps

When analyzing logs, you might want to know how much time passed between two events:

$logTime1 = Get-Date "2025-11-13 08:15:00"
$logTime2 = Get-Date "2025-11-13 09:00:00"
$diff = $logTime2 - $logTime1

Write-Host "Time difference between log events: $($diff.TotalMinutes) minutes"

Check out Create a Folder with the Current Date using PowerShell

Handling Time Zones and UTC

When working across multiple systems or regions, time zones can cause inconsistencies. PowerShell provides options to handle this.

Convert to UTC

$utcNow = (Get-Date).ToUniversalTime()

Convert from UTC to Local Time

$localTime = $utcNow.ToLocalTime()

Always ensure both dates are in the same time zone before calculating differences. For example:

$startUTC = (Get-Date "2025-11-13T08:00:00Z")
$endUTC   = (Get-Date "2025-11-13T09:30:00Z")

$diff = $endUTC - $startUTC
Write-Host "Difference in minutes (UTC): $($diff.TotalMinutes)"

Error Handling and Validation

When working with user inputs or external data, always validate your dates to avoid runtime errors.

Example with Validation

Here is an example, along with the complete PowerShell script with validation.

try {
    $startDate = [datetime](Read-Host "Enter start date (MM/dd/yyyy HH:mm)")
    $endDate   = [datetime](Read-Host "Enter end date (MM/dd/yyyy HH:mm)")

    if ($endDate -lt $startDate) {
        throw "End date cannot be earlier than start date."
    }

    $diff = $endDate - $startDate
    Write-Host "The difference is $([math]::Round($diff.TotalMinutes)) minutes."
}
catch {
    Write-Host "Error: $_" -ForegroundColor Red
}

This ensures your script handles invalid inputs gracefully.

Conclusion

In this tutorial, I explained how to get the difference between two dates in minutes in PowerShell with examples.

Here I explained the below things:

  • Use Get-Date to create or retrieve date objects.
  • Use New-TimeSpan -Start -End or simply subtract the two dates.
  • Access the .TotalMinutes property to get the difference in minutes.
  • Use rounding and formatting for user-friendly output.
  • Always handle time zones and validation.

You may also like:

Leave a Comment

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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