Calculate Time Difference between Dates in PowerShell

access_time 3 years ago visibility15855 comment 0

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

image

The C# way

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

Output

image

info Last modified by Raymond 3 years ago copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Follow Kontext

Get our latest updates on LinkedIn or Twitter.

Want to publish your article on Kontext?

Learn more

More from Kontext

visibility 492
thumb_up 0
access_time 3 years ago

The following is a PowerShell code snippet to start multiple VMs through vmplayer.exe: # Navigate to the folder where vmplayer.exe file exists cd "D:\Program Files (x86)\VM Player" # Start the VMs by using vmplayer.exe <# vmplayer.exe command help vmplayer.exe [OPTION…] [–] ...

visibility 15855
thumb_up 1
access_time 3 years ago

PowerShell provides a number of cmdlets to retrieve current date time and to create time span object. $current = Get-Date $end= Get-Date $diff= New-TimeSpan -Start $current -End $end Write-Output "Time difference is: $diff" $current = [System.DateTime]::Now $end= ...

visibility 74
thumb_up 0
access_time 2 years ago

This code snippet shows how to calculate time differences.