简体   繁体   中英

Subtracting hours from Get-Date/DateTime Object

I'm studying fundamentals on Powershell to avoid some silly misconceptions so I'm working on some misc projects. I want to take the Cisco Interval Values (15 minute intervals over 24 hours = 96 intervals) and output the time of the error.

For instance, if the interval in question is 2 and it is 11:00 AM currently, the interval time would be yesterday at 11:30 AM.

What I have thus far is just the equation, but I'm not sure how to get the DateTime object to a point that I can manipulate it for the equation.

$myInt = Read-Host "Interval Number";
$lapsed =  ([int] $myInt - [int]96) * [int]15 / 60
$currentTime = Get-Date

If possible, I'd like to essentially take my current time, subtract the number of hours, then create a new time from that.

Solution Code:

$myInt = Read-Host "Interval Number";
$interval =  ([int] $myInt - [int]96) * [int]15 / 60
$currentTime = Get-Date 
"Current time is: $currentTime"

$elapsedTime = $currentTime.AddHours(-$lapsed)
"Interval $myInt reported at $elapsedTime"

My solution was using AddHours() Get-Date.AddHours() to subtract the number of hours elapsed in the intervals.

$myInt = Read-Host "Interval Number";
$interval =  ([int] $myInt - [int]96) * [int]15 / 60
$currentTime = Get-Date 
"Current time is: $currentTime"

$elapsedTime = $currentTime.AddHours(-$lapsed) ## Subtract the value of $lapsed from $currentTime
"Interval $myInt reported at $elapsedTime"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM