Calculate Time Difference between Dates in PowerShell

Raymond Tang Raymond Tang 0 40407 15.21 index 3/27/2018

PowerShell provides a number of cmdlets to retrieve current date time and to create time span object.

Calculate time difference - CmdLets

$current = Get-Date
$end= Get-Date
$diff= New-TimeSpan -Start $current -End $end
Write-Output "Time difference is: $diff"

Output

https://api.kontext.tech/resource/175ac5ff-49bf-543e-89d9-ad2b38be7f36

The C# way

$current = [System.DateTime]::Now
$end= [System.DateTime]::Now
$diff= $end.Subtract($end)
Write-Output "Time difference is: $diff"

Output

https://api.kontext.tech/resource/70cb5e24-ad38-5096-880c-02e1c54b7483

lite-log powershell

Join the Discussion

View or add your thoughts below

Comments