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

208,423

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')
Share:
208,423

Related videos on Youtube

Ike Walker
Author by

Ike Walker

Updated on July 16, 2022

Comments

  • Ike Walker
    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 before 2013-03-11 0:35 CDT was 2013-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
      tink about 11 years
      Are you always running this at the same time, are you running it repeatedly?
    • Ike Walker
      Ike Walker about 11 years
      @tink it runs daily at 00:35
    • shgnInc
      shgnInc almost 10 years
  • user456584
    user456584 almost 10 years
    If interested in using the GNU style within OSX, you can also use gdate, available within homebrew's coreutils package.
  • augurar
    augurar about 8 years
    You mean date is different.
  • null
    null almost 8 years
    How would you assign this to a variable for use later on?
  • tink
    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
    Mikko Rantalainen almost 8 years
    Stuff like this is needed if you need to deal with Mac OS X date which does not support yesterday syntax...
  • gogstad
    gogstad about 7 years
    For GNU-date: date -d yesterday 13:00 -I
  • Nicolas Raoul
    Nicolas Raoul about 7 years
    This relies on the fact that switching between summer and winter time is always done during night, if I understand correctly?
  • Nicolas Raoul
    Nicolas Raoul about 7 years
    What is the meaning of the space between 13:00 and "? The command seems to work without it.
  • tink
    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
    Nicolas Raoul about 7 years
    @tink: Is it always at 2AM and 3AM, in all countries?
  • Piskvor left the building
    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
    Oliver Hankeln almost 5 years
    This 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
    isapir over 4 years
    You should change your format to %Y%m%d to match the question. I upvoted anyway as it worked properly.
  • SherylHohman
    SherylHohman almost 4 years
    While 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
    Vishrant over 2 years
    This is a valid solution when I am using BusyBox v1.31.1
  • TheEsnSiavashi
    TheEsnSiavashi over 2 years
    Perfect 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' )'