Skip to content
Advertisement

bash date, create a new object on a specific timestamp that is n hours later from now

I am struggling to understand how to set a variable to be a particular timestamp.

given

dt=$(date);

dt which is the current date, say 2016-05-11 17:10:00, i want to create another date variable that is (roughly) 9 hours after dt, and on the exact hour, i.e., 2016-05-12 02:00:00.

In other words, I would set the new date to be 9 hours later, then set its minute and second to 0.

Advertisement

Answer

You can use -d which lets you set a date, but also supports things like: yesterday, 5 hours ago and +5 hours. Don’t know the portability though.

And then format for your needs:

date -d '+9 hours' +'%Y-%m-%d %H:00:00'
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement