Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding down minutes to the nearest 10 minutes

Tags:

powershell

I am using powershell to get the date and time ten days ago. To do this I am using the following

$date10DaysAgo = (get-date).adddays(-10).tostring("yyyy-MM-dd HH:mm")

This is part of a script that runs every half hour. Sometimes there is a delay in running the script of about a minute but I want the time part of the string to show the hour or half hour. So If this ran at 16:31 I would need it to show 16:30 and if it ran at 17:01 it would show 17:00.

like image 880
Silentbob Avatar asked Oct 23 '25 04:10

Silentbob


1 Answers

You can do this by subtracting $date10DaysAgo.Minute % 10 from the minutes:

$date10DaysAgo = (get-date).adddays(-10)
$date10DaysAgo = $date10DaysAgo.AddMinutes(- $date10DaysAgo.Minute % 10).toString("yyyy-MM-dd HH:mm")
like image 129
Tony Hinkle Avatar answered Oct 25 '25 19:10

Tony Hinkle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!