Get Yesterday's Date in Bash on Linux, Dst-Safe

Get yesterday's date in bash on Linux, DST-safe

I think this should work, irrespective of how often and when you run it ...

date -d "yesterday 13:00" '+%Y-%m-%d'

Get the date (a day before current time) in Bash

Sorry not mentioning I on Solaris system.
As such, the -date switch is not available on Solaris bash.

I find out I can get the previous date with little trick on timezone.

DATE=`TZ=MYT+16 date +%Y-%m-%d_%r`
echo $DATE

getting a previous date in bash/unix

Several solutions suggested here assume GNU coreutils being present on the system. The following should work on Solaris:

TZ=GMT+24 date +’%Y/%m/%d’

Detect DST from a date entered by user in bash

You can get GNU date to tell you the time zone abreviation. This will change if DST is in effect:

TZ=US/Pacific date --date '2015/05/12' +%Z

Returns:

PDT

And:

TZ=US/Pacific date --date '2015/12/12' +%Z

Returns:

PST

I don't see a way to get whether DST is in effect explicitely from date, but depending on what you are trying to accomplish this may solve your problem.

How can I get and format yesterday's date on the command line?

GNU date:

date --date='yesterday' '+%Y%m%d'

Yesterday's date variable in BASH on AIX Server

You attempted

yesterday=$(echo -e "$(TZ=GMT+28 date +%Y%m%d)\n$(TZ=GMT+18 date +%Y%m%d)|
grep -v $(date +%Y%m%d)|sort|tail -1)

I think it worked.

Get Yesterday's date in solaris

Try this below thing. It should work

YESTERDAY=`TZ=GMT+24 date +%Y%m%d`; echo $YESTERDAY

Print the path of a file a day before and a day after in Shell script

To get tomorrow's data, you can do:

date -d '+1 day' "+%Y-%m-%d"

To get yesterday's data, you can do:

date -d '-1 day' "+%Y-%m-%d"

To use it in script:

#!/bin/bash

nextDate=$(date -d '+1 day' "+%Y-%m-%d")
prevDate=$(date -d '-1 day' "+%Y-%m-%d")

nextDatePath=/home/$USER/logging/${TIMESTAMP}/status/${nextDate}.fail_log

prevDatePath=/home/$USER/logging/${TIMESTAMP}/status/${prevDate}.fail_log

UNIX - Date format for yesterday

Your call to date is actually working fine.

The real problem is that in your bash script (which is in the comments to another answer) you are performing some arithmetic on the resulting values, and the subsequent concatenation of those values loses the leading zeros.

So, in your bash script, after you've calculated the new values of $YEAR, $MONTH and $DAY, use this to get the right output filename:

SOURCEFILE=`printf "DNXOUT-%04d%02d%0d2.txt" $YEAR $MONTH $DAY`

i.e. just use the printf command line executable (which probably does exist) to format the filename.



Related Topics



Leave a reply



Submit