Calculate Time Difference between Dates in PowerShell

2018-03-27 lite-logpowershell

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

/project/scripting/resources/175AC5FF-49BF-543E-89D9-AD2B38BE7F36.webp

The C# way

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

Output

/project/scripting/resources/70CB5E24-AD38-5096-880C-02E1C54B7483.webp