Mohit Rane
Mohit Rane

Reputation: 279

Shell script to round off time to nearest 5 minutes

date=$(date +%Y_%m_%d_%H_%M)
echo "CURRENT DATE-->" "$date"

How to round it off to the nearest five minutes? For example, the output should be:

2016_01_20_17_20

Upvotes: 1

Views: 1599

Answers (1)

glenn jackman
glenn jackman

Reputation: 246837

date -d @$(( (($(date +%s) + 150) / 300) * 300)) "+%Y_%m_%d_%H_%M"

The inner date call returns the epoch time in seconds, and then rounds (up or down) to nearest multiple of 300 (5 minutes). The outer date call formats as desired.

Upvotes: 4

Related Questions