Welcome › Forums › General PowerShell Q&A › Adding hours minutes and seconds to a time value.
This topic contains 8 replies, has 5 voices, and was last updated by
-
AuthorPosts
-
November 1, 2017 at 3:34 pm #83387
I have a time value such as '23:10:00' to which I need to add, say, 10:00:01, but I am not interested in the time of day, just the total. Using
(Get-Date '23:10:00').AddHours(10)
usually produces 07:10:00, ie the next day. How to I simply arrive at the total 30:10:01 without adding each individual part?
-
November 1, 2017 at 3:39 pm #83390
Take your $originalTime, and create $newTime by adding your days (or whatever) to it. Then subtract $newTime – $originalTime.
The result of the subtraction will be a System.TimeSpan, which has methods for expressing the time span as hours or days or whatever you want.
-
November 1, 2017 at 3:45 pm #83395
Many thanks. I tried this and there was a Timespan returned, but it just showed the total I originally added. What I need is the sum of the two. I am sorry if I am missing something very basic here.
-
November 1, 2017 at 4:10 pm #83405
(get-date).hour + 10
That will return the hour as an integer which you could then add hours to get a running total. Without knowing the purpose of the result, it's hard to suggest other solutions.
If you want to use a date/time object, it's concerned about the date and time, so I don't think you'll find a way without adding hours, minutes and seconds separately.
-
November 1, 2017 at 5:06 pm #83425
How's that? It rounds it to days though. You can leave out the second [timespan].
[timespan]'23:10:00' + [timespan]'10:00:01'
-
November 1, 2017 at 11:22 pm #83447
This is workable, as I can format the TotalHours,TotalMinutes,TotalSeconds returned to a usable result. As I said earlier the result I am after here is 33:10:01 in this example. I cannot use (Get-Date) as it returns an hour from the next day if the total exceeds 24 hours.
I have now cobbled together a working script which demonstrates what I am trying to achieve; and just for good measure there is also a subtract function:[Int]$h = [Int]$m = [Int]$s = 0 function Add ([String]$test) { $temp = $test.Split(':') $SCRIPT:h+= $temp[0] $SCRIPT:m+= $temp[1] $SCRIPT:s+= $temp[2] if ($SCRIPT:s -gt 59) { [Int]$min = $SCRIPT:s/60 [Int]$sec = $SCRIPT:s%60 $SCRIPT:m+= $min $SCRIPT:s = $sec } if ($SCRIPT:m -gt 59) { [Int]$hour = $SCRIPT:m/60 [Int]$min = $SCRIPT:m%60 $SCRIPT:h+= $hour $SCRIPT:m = $min } } function Subtract ([String]$test) { $minus = $test.Split(':') if (($SCRIPT:s - [Int]$minus[2]) -lt 0) { $SCRIPT:s+= (60 - [Int]$minus[2]) [Int]$minus[1]+= 1 } else { $SCRIPT:s = $SCRIPT:s - [Int]$minus[2] } if (($SCRIPT:m - [Int]$minus[1]) -lt 0) { $SCRIPT:m+= (60 - [Int]$minus[1]) [Int]$minus[0]+= 1 } else { $SCRIPT:m = $SCRIPT:m - [Int]$minus[1] } $SCRIPT:h = $SCRIPT:h - $minus[0] } Add '23:43:15' Add '00:26:25' Add '12:23:29' $result = "{0:D3}:{1:D2}:{2:D2}" -f $h, $m, $s Write-Host $result if ($result -gt '10:45:08') { #No point trying if too large a value. Subtract '10:45:08' $result = "{0:D3}:{1:D2}:{2:D2}" -f $h, $m, $s Write-Host $result }
If there is a simpler way I would much appreciate it if one of you experts could please enlighten me.
-
November 2, 2017 at 1:41 pm #83480
This one is simpler:
$now = Get-Date '23:10:00' $then = $now.AddHours(10).AddMinutes(0).AddSeconds(1) $timespan = $then - $now $hrs=[int]$now.Hour+[int]$timespan.Hours "{0:D3}:{1:D2}:{2:D2}" -f $hrs,$then.Minute,$then.Second
-
November 2, 2017 at 2:02 pm #83482
$result = [timespan]'23:10:00' + '10:00:01' '{0:d2}:{1:d2}:{2:d2}' -f [int]$result.totalhours, $result.minutes, $result.Seconds 33:10:01
[timespan]'33:10:01' is 33 days though.
-
November 2, 2017 at 11:42 pm #83539
But this fails:
$result = [timespan]'24:10:00' + '10:00:01'
After adding a bunch of times I often exceed 24 hours so cannot use this one. Likewise Get-Date '24:10:00' throws an error.
-
AuthorPosts
The topic ‘Adding hours minutes and seconds to a time value.’ is closed to new replies.