Command Line Date – Get Current 15 Minute Interval

command linedate

Is there any way I can get the current 15 minute interval using the date command or similar ?

e.g. something like date %Y.%m.%d %H:%M will give me 2011.02.22 10:19 , I need something that yields 2011.02.22 10:15 in the time span from 10:15 to 10:29.

Best Answer

You can get the current unix timestamp with date "+%s", find the current quarter-hour with some simple math (my example is in bash) and print it back with a better format with date :

curdate=`date "+%s"`
curquarter=$(($curdate - ($curdate % (15 * 60))))
date -d"@$curquarter"

The @ syntax to set the current date and time from a timestamp is a GNU extention to date, if it don't works on your OS, you can do the same like this (don't forget to specify UTC, otherwise, it won't work) :

date -d"1970-01-01 $curquarter seconds UTC"
Related Question