I have a simple question.
date '+%Y%m%d' --date='20130417 2 day ago'
20130415
works fine.
I have an env var
today="20130417"
but the following command does not work.
date '+%Y%m%d' --date='$today 2 day ago'
any workarounds?
You need double quotes instead of single:
$ date '+%Y%m%d' --date="$today 2 day ago"
20130415
Otherwise, the values within --date=' ' don't get evaluated.
This is a general behaviour, see an example:
$ echo 'the date is: $today'
the date is: $today
$ echo "the date is: $today"
the date is: 20130417
Related
I see that unix date util provides a way to create a date from a string with specified format.
And it can also create a date using such strings as "last monday" or X days ago.
Is it possible to get a last monday for any given date using just date util and bash.
Something akin
date -d "$(date -d '-24 day' +%Y%m%d) last monday"
This may not be your desired answer as it does not use the string such as last monday but how about:
given="Mar 16" # example
dow=$(date -d "$given" +%w) # day of week (0: Sun .. 6: Sat)
before=$(( (dow + 5) % 7 + 1 )) # days to go back
date -d "$given -$before days" +%Y%m%d # result
I'm trying to figure out how to get the current hour rounded down to start of the hour and the next hour in bash?
For example, if I run my script:
./printHour.sh
and let's say the current time at execution is 13:04:12 - it would print
current hour is: 13:00:00
next hour is: 14:00:00
Progress so far: (but this gives 1hour ago so it does not work) - any ideas?
lastHour=$(date -d '1 hour ago' "+%H:%M:%S")
echo "current hour is: "$lastHour
You can use this utility function:
hrdt() { date -d "${1?} hour ago" '+%H:00:00'; }
Testing:
> hrdt
bash: 1: parameter not set
> hrdt 0
08:00:00
> hrdt 1
07:00:00
> hrdt 2
06:00:00
> hrdt 3
05:00:00
Could you please try following, written and tested as per shown samples, my date is GNU date version.
cat script.bash
#!/bin/bash
currentHour=$(date "+%H:00:00")
nextHour=$(date -d '+1 hour' "+%H:00:00")
echo "current hour is: $currentHour"
echo "next hour is: $nextHour"
When I run above script I get as follows:
current hour is: 06:00:00
next hour is: 07:00:00
Seems like you don't need anything special so this should do it:
date -d '1 hour ago' "+%H:00:00"
Why bother when you want exactly the hour where both %M and %S are expected to be zero?
You don't need date in this case; as seen below, built-in printf can generate formatted date-time strings too. Here -1 represents current time, and EPOCHSECONDS is a dynamic variable that expands to the number of seconds since epoch.
$ printf 'current hour is: %(%H)T:00:00\n' -1
current hour is: 17:00:00
$
$ printf 'next hour is: %(%H)T:00:00\n' $((EPOCHSECONDS + 3600))
next hour is: 18:00:00
Using awk,
$ awk ' BEGIN { st=systime();
print "current hour=" strftime("%F %H:00:00",st);
print "next hour=" strftime("%F %H:00:00",st+(60*60)) } '
current hour=2020-12-26 23:00:00
next hour=2020-12-27 00:00:00
$
I have problem that is using custom date format like date +%y%j.%H%M%S,
And what i want is to add 15 mins on the this date on just on current date of system. so that i can use for further calculation into my process.
I have tried with below code -
$uprBond=`date +%y%j.%H%M%S`
$ echo $uprBond
16079.031135
$ date -d "$(uprBond) + 5 minutes" +%y%j.%H%M%S
op > bash: uprBond: command not found
16079.035920
I am failing while passing the above date format , Can anybody please help on this.
Just for note, below is the piece of code is working when i used date function instead of defined date variable i.e. $uprBond (I don't want to use predefined date because we have some old same formatted date which needs that adding of mins).
date +%y%j.%H%M%S -d "`date` + 5 minutes";
op > 16079.040724
With GNU date, GNU bash 4 and its Parameter Expansion:
#!/bin/bash
uprBond="$(date +%y%j.%H%M%S)"
year="20${uprBond:0:2}"
doy="${uprBond#${uprBond:0:2}}"
doy="${doy%.*}"
time="${uprBond#*.}"
time="${time:0:2}:${time:2:2}:${time:4:2}"
in5min=$(date -d "${year}-01-01 +${doy} days -1 day +5 minutes ${time}" "+%y%j.%H%M%S")
echo "now: $uprBond"
echo "in 5min: $in5min"
Output:
now: 16079.145026
in 5min: 16079.145526
I'm trying to make some calculations with date in bash script but can't find out the right syntax. I get a string from a file that I convert to a date. Then I want to get the date for one and two days ahead. Looking on another StackOverflow posts it looked easy adding days to today date. This is what I am doing now:
# Extract date string from file
ctldate=`awk 'NR==8 { print $4 }' a-AC-2015-02-10-120000-g3.ctl`
echo $ctldate
12:00Z10feb2015
# Convert string to date
ctldate2=`date +'%d/%m/%Y' -d $ctldate`
echo $ctldate2
10/02/2015
# Try to add a day, should be 11/02
data1=$(date +'%d/%m/%Y' -d "$ctldate" --date='1 day')
echo $data1
12/02/2015
# Also tried
data1=$(date +'%d/%m/%Y' -d "$ctldate2" --date='1 day')
echo $data1
12/02/2015
# And
data1=`date +'%d/%m/%Y' -d $ctldate --date='1 day'`
echo $data1
12/02/2015
It seems that I'm not properly passing $ctldate var to command and that the base date for calculation is today.
Thanks in advance
When you pass several -d or --date, date uses the last one. So when you do:
date -d "$ctldate" --date='1 day'
date will take into account --date='1 day' as it's the last -d/--date argument, and will happily show you tomorrow's date.
Instead you should use:
date -d "$ctldate +1 day"
Is there any built-in function to get next Sunday (next Monday, Tuesday, etc.) from a given date using bash shell script?
For example, what is the first Sunday after 1-Sep-2014? I expect 7-Sep-2014.
I have searched the answer in google, but only found this function :
date "+%Y%m%d" -d Sun
which is to get next Sunday after today.
I fear date can't do it directly. But you can use brace expansion to get the dates for all the following 7 days and select Sunday from them:
echo '1-Sep-2014\ +'{1..7}'\ days' | xargs -n1 date -d | grep Sun
You can use this little script:
#!/bin/bash
w=$(date -d"$1" +%w)
diff=$(( (7-$w)%7 ))
date -d"+$diff day $1" +%F
saved as sun.sh then:
kent$ ./sun.sh 01-Sep-2014
2014-09-07
kent$ ./sun.sh 31-Aug-2014
2014-08-31
kent$ ./sun.sh 3-Sep-2014
2014-09-07
kent$ ./sun.sh 23-Sep-2014
2014-09-28
Note that, argument validation was not done, I just showed how to calculate the date.
It is possible to make date do all the math without using shell code for math functions or to iterate though output looking for answers. The date function can do basic math like "x + 1 days" or "x - 1 month + 2 days".
It can also output formatted strings with arbitrary values.
The trick here is to use one date invocation to output the string used to calculate your final answer. We'll input your input date and ask for what day of the week that is, but at the same time output some math to subtract that number of days. By adding 7 days first we'll end up an the next week, then by subtracting the day-of-the-week number from that we'll have the next Sunday:
INPUTDATE="20140901"
date +"%Y%m%d" -d "$(date +"%Y%m%d + 7 days - %w days" -d "$INPUTDATE")"
This will spit out your expected answer: 20140907. Of course you can change the output format using the format string on the outer date invocation.