Rounding a date in PowerShell

Was involved in an interesting post about grouping data based on the date. Now obviously you can’t just use Group-Object on the date because by default it has it down to the second and that’s not going to group anything. So we can do a little rounding.

$dt = get-date

$dt.addminutes(-($dt.minute % 5))

This will round it down to the closest 5 min marker. You can swap out the minutes for hours or seconds or whatever unit you want as well as change the number.

If you want to use this in a select, you can do something like this.

$myobject | select name, something, @{l="date";e={$_.addminutes(-($_.minute % 5))}



About jrich

I am the Solutions Architect for Apex Learning in Seattle WA. I've been working with computers since I was 13. Started programming when I was 14. Had my first IT job as tech support at an ISP at the age of 15 and became a network admin at the age of 17. Since then I've worked at a variety of small to mid size companies supporting, maintaining and developing all aspects of IT. Mostly working with Windows based networks but have recently been working with Solaris system as well. I created this blog mostly as a place for me to take my own notes, but also share things that I had a hard time finding the info for.

Posted on October 3, 2011, in WMF (Powershell/WinRM) and tagged , . Bookmark the permalink. 4 Comments.

  1. Another method I’ve used is to use OADate. This is a decimal number that represents whole days, and you can slice off decimal fractions of a day just by truncating or rounding the number.

  2. This was EXACTLY what I was looking for. Thank you! I had no idea about the “% 5” command. Perfect. My previous solution was about 5 lines of code and very clunky. Thank you again for posting this.

    Keep up the good work.

  3. im looking to round the timestamp to 00:00 that day. i have
    $Date = Get-Date -format yyyyMMddHHmmss
    $Ndate = $date.addhours(-($date.hours %24))

    i get

    Method invocation failed because [System.String] does not contain a method named ‘addhours’.
    At line:2 char:1
    + $Ndate = $date.addhours(-($date.hours %24))
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

    any help would be greatfull.

    • The error message tells you why, but, if you take a look at what you have.. $date.gettype() you’ll see its a string and not a datetime object. If you take a look at the help for Get-Date you’ll notice that -Format has a comment about the output. Honestly that comment is horrible because it states that some of the properties of it might be gone, when in reality what its doing is converting it to a string, because you told it to look pretty.

      The short of it is, dont use the -format when you get the date, you can format it when/if you display it.
      For the same reason you never save the output of a format-table (or format-* anything really) because its special output, and not the raw data any more.
      You should take a look at my string posts

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: