bash - How can I use addhour for custom date? -
i have string conf file (lets call example date1):
#!/bin/bash # example date1="201605250925" datenow="$(date +%y%m%d%h%m -d "+1hour")" date2=$(date +%y%m%d%h%m -d "$date1 + 1hour") # not work? echo "$date1 --> $date2" # work! echo "$date1 --> $datenow"
i need add 1 hour. getting error this:
date: invalid date `201605250925 + 1hour'
but work datenow
.
how can user addhour custom date format string?
you need format meets command date
expectations, like:
2016-05-25 09:25
the space denote start of time , time format hh:mm.
comes international iso 8601, using space instead of t.
if format fixed, can use bash internal capacities (no external command except date used) change this:
#!/bin/bash d1="201605250925" dc="${d1:0:8} ${d1:8:2}:${d1:10:2}+0" d2=$(date +'%y%m%d %h:%m' -ud "$dc + 1 hour" ) echo "$d2"
or posixly (dash) no call needed sed, awk or cut (faster):
#!/bin/dash d1="201605250925" dt=${d1##????????} dc="${d1%%"$dt"} ${dt%%??}:${dt##??}+0" d2=$(date -ud "$dc + 1 hour" +'%y%m%d %h:%m') echo "$d2"
20160525 10:25
the inclusion of +0
after time in dc
: 20160525 09:25+0
ensure date
interpret time offset 0 (utc).
the use of option -u
date ensure value read in utc change in utc, avoiding daylight correction or local time change.
Comments
Post a Comment