Quantcast
Channel: PowerShell
Viewing all articles
Browse latest Browse all 15370

Time Difference in Powershell

$
0
0

I wanted to post this solution to a Powershell problem I had because I couldn't find any examples on the internet.

I needed to be able to calculate the difference between a timestamp from the last 24 hours, and the current time.

The input and output formats needed to be hh:mm:ss

Here's how I did it:

Powershell
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$Time1="15:30:00"$Time2=Get-Date-formatHH:mm:ss$TimeDiff=New-TimeSpan$Time1$Time2if($TimeDiff.Seconds-lt0){$Hrs=($TimeDiff.Hours)+23$Mins=($TimeDiff.Minutes)+59$Secs=($TimeDiff.Seconds)+59}else{$Hrs=$TimeDiff.Hours$Mins=$TimeDiff.Minutes$Secs=$TimeDiff.Seconds}$Difference='{0:00}:{1:00}:{2:00}'-f$Hrs,$Mins,$Secs$Difference


I hope this helps someone else.

Russ


Viewing all articles
Browse latest Browse all 15370

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>