Get yesterday's date in bash on Linux, DST-safe
Solution 1
I think this should work, irrespective of how often and when you run it ...
date -d "yesterday 13:00" '+%Y-%m-%d'
Solution 2
Under Mac OSX date works slightly different:
For yesterday
date -v-1d +%F
For Last week
date -v-1w +%F
Solution 3
This should also work, but perhaps it is too much:
date -d @$(( $(date +"%s") - 86400)) +"%Y-%m-%d"
Solution 4
If you are certain that the script runs in the first hours of the day, you can simply do
date -d "12 hours ago" '+%Y-%m-%d'
BTW, if the script runs daily at 00:35 (via crontab?) you should ask yourself what will happen if a DST change falls in that hour; the script could not run, or run twice in some cases. Modern implementations of cron
are quite clever in this regard, though.
Solution 5
date -d "yesterday" '+%Y-%m-%d'
To use this later:
date=$(date -d "yesterday" '+%Y-%m-%d')
Related videos on Youtube
Ike Walker
Updated on July 16, 2022Comments
-
Ike Walker almost 2 years
I have a shell script that runs on Linux and uses this call to get yesterday's date in
YYYY-MM-DD
format:date -d "1 day ago" '+%Y-%m-%d'
It works most of the time, but when the script ran yesterday morning at
2013-03-11 0:35 CDT
it returned"2013-03-09"
instead of"2013-03-10"
.Presumably daylight saving time (which started yesterday) is to blame. I'm guessing the way
"1 day ago"
is implemented it subtracted 24 hours, and 24 hours before2013-03-11 0:35 CDT
was2013-03-09 23:35 CST
, which led to the result of"2013-03-09"
.So what's a good DST-safe way to get yesterday's date in bash on Linux?
-
tink about 11 yearsAre you always running this at the same time, are you running it repeatedly?
-
Ike Walker about 11 years@tink it runs daily at 00:35
-
shgnInc almost 10 yearspossible duplicate of Get the date (a day before current time) in Bash
-
-
user456584 almost 10 yearsIf interested in using the GNU style within OSX, you can also use
gdate
, available within homebrew'scoreutils
package. -
augurar about 8 yearsYou mean
date
is different. -
null almost 8 yearsHow would you assign this to a variable for use later on?
-
tink almost 8 years@null - same way you'd assign the output of any other shell command. variable=$( date -d "yesterday 13:00 " '+%Y-%m-%d' ) ...
-
Mikko Rantalainen almost 8 yearsStuff like this is needed if you need to deal with Mac OS X
date
which does not supportyesterday
syntax... -
gogstad about 7 yearsFor GNU-date:
date -d yesterday 13:00 -I
-
Nicolas Raoul about 7 yearsThis relies on the fact that switching between summer and winter time is always done during night, if I understand correctly?
-
Nicolas Raoul about 7 yearsWhat is the meaning of the space between
13:00
and"
? The command seems to work without it. -
tink about 7 years@Nicolas Raoul: that space is a typo, doesn't need to be there. And yes, switching to / from daylight saving happens at 2 and 3 respectively.
-
Nicolas Raoul about 7 years@tink: Is it always at 2AM and 3AM, in all countries?
-
Piskvor left the building about 7 years@NicolasRaoul: It is not guaranteed; but it is a widely-followed convention to schedule this into early morning. On the other hand, there is IIRC no place where the switch would happen close to the local noon. So, "1300J is never on a DST boundary" is a reasonable assumption.
-
Oliver Hankeln almost 5 yearsThis is not assuming that the switch between summer and winter time happens before 13:00 - it just assumes that today 13:00 - 24 hours is a time yesterday. So if the jump between summer and winter time is less than 11 hours, this should aways work.
-
isapir over 4 yearsYou should change your format to
%Y%m%d
to match the question. I upvoted anyway as it worked properly. -
SherylHohman almost 4 yearsWhile this code may resolve the OP's issue, it's better to include an explanation on how your code addresses the OP's issue. This way, future visitors can learn from your post, & apply it to their own code. SO is not a coding service, but a resource for knowledge. High quality, complete answers reinforce this idea, and are more likely to be upvoted. These features, plus the requirement that all posts be self-contained, are some strengths of SO as a platform that differentiates us from forums. You can edit to add additional info &/or to supplement your explanations with source documentation.
-
Vishrant over 2 yearsThis is a valid solution when I am using
BusyBox v1.31.1
-
TheEsnSiavashi over 2 yearsPerfect answer! My case was to set the variable to the previous Monday and a similar approach worked: '$( date -d "last Monday 13:00 " '+%Y%m%d' )'