locked
Math function (for use in PS script) to determine exact 5 minute intervals starting at top of the hour RRS feed

  • Question

  • This is more of a math question than PS, but I am wondering if anyone has seen or created a math function that will determine the next 5 minute time slice from starting at the top of the last hour.  Not sure how to explain what I need (which has made searching for this with Google difficult), but let me give an example:

    If the time is 1:03 PM, I want a math function to spit out 1:05 PM (not 1:08 PM, which is 5 minutes from the current time)

    If the time is 1:48 PM, I want that math function to spit out 1:50 PM (not 1:53 PM)

    If you could provide this in PS format, that would be best - but if not, even if you could provide the math technique to figure this out, I can convert it to PS myself.

    Thanks

    NK

    Monday, September 29, 2014 6:06 PM

Answers

  • Something like this, perhaps?


    $minuteIncrement = 5
    $dt = get-date
    $dt = $dt.AddSeconds(-$dt.Second)
    $minutesToAdd = $minuteIncrement - ($dt.Minute % $minuteIncrement)
    $dt.AddMinutes($minutesToAdd)
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by NJK-Work Monday, September 29, 2014 6:29 PM
    Monday, September 29, 2014 6:24 PM

All replies

  • Something like this, perhaps?


    $minuteIncrement = 5
    $dt = get-date
    $dt = $dt.AddSeconds(-$dt.Second)
    $minutesToAdd = $minuteIncrement - ($dt.Minute % $minuteIncrement)
    $dt.AddMinutes($minutesToAdd)
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by NJK-Work Monday, September 29, 2014 6:29 PM
    Monday, September 29, 2014 6:24 PM
  • While this probably isn't the cleanest or best way, it's one way. 

    function get-next-five-minute-span($time) {
        $time = (get-date $time).AddMinutes(5)
        $match =@('00','05','10','15','20','25','30','35','40','45','50','55')
    
        do {
            if ( $match.Contains([string](get-date($time) -Format mm)) ){
                break
            } else {
                $time = (get-date($time).AddMinutes(-1))
            } 
        } while ($true)
        return $time
    }

    Monday, September 29, 2014 6:27 PM
  • Thank you sir - that works perfect!

    NK

    Monday, September 29, 2014 6:30 PM
  • Thank you!.  Going to go with Bills suggestion as it is a bit cleaner - but I appreciate you taking the time to offer your solution.

    NK

    Monday, September 29, 2014 6:30 PM