Blog Archives
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))}
Enjoy!
