UNIX - Date format for yesterday

11,881

Solution 1

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.

Solution 2

Have you tried: date +%Y%m%d.

$ date +%Y%m%d
20120110

$ date -d yesterday +%Y%m%d
20120109
Share:
11,881
Dan
Author by

Dan

Updated on June 04, 2022

Comments

  • Dan
    Dan almost 2 years

    I know there's some answers elsewhere on this site, and others, but can anyone help me get my date in format yyyymmdd, when I use anything I've found on different forums, I just get yyyymmd, my script then fails for the first 9 days of the month...

    The script I have at the moment uses bash and get the date using

    date '+%m %d %Y'
    

    However, this returns 20120110 for today, but for yesterday it returned 2012019, I need this to return 201201*0*9.

    Any ideas?

    Thanks

    P.S. I can't install anything else onto this server, so gnu or perl are out.

  • Dan
    Dan over 12 years
    Yeah, sorry, the script uses each part to build up the date for yesterday:- date '+%m %d %Y' | { read MONTH DAY YEAR DAY=expr "$DAY" - 1 case "$DAY" in 0) MONTH=expr "$MONTH" - 1 case "$MONTH" in 0) MONTH=12 YEAR=expr "$YEAR" - 1 ;; esac DAY=cal $MONTH $YEAR | grep . | fmt -1 | tail -1 esac NOW=$YEAR$MONTH$DAY SOURCEFILE="DNXOUT-$NOW.txt"
  • Dan
    Dan over 12 years
    Not sure if that makes a difference, but at the moment, it's creating the variable as "DNXOUT-2012019.txt"
  • dogbane
    dogbane over 12 years
    "-d yesterday" returns yesterday's date. It looks like your version of date does not support this flag. But that doesn't matter. I was just trying to demonstrate what would be output if it was yesterday.
  • Dan
    Dan over 12 years
    For some reason it doesn't display as yours does, maybe there's a newer version that it works on, but not mine. Oh well, guess it'll have to be a manual process for the 1st 9 days of the month! :-)
  • Alnitak
    Alnitak over 12 years
    @Dan it's the Bash script calculations to work out "yesterday" that are dropping the leading zero, not the date command.
  • Alnitak
    Alnitak over 12 years
    @Dan they will also fail for the month field on the first of October.
  • Dan
    Dan over 12 years
    Maybe I'm missing something, but this just seems to set my SOURCEFILE to "printf "DNXOUT-%04d%02d%0d2.txt" $YEAR $MONTH $DAY"?
  • Dan
    Dan over 12 years
    Is there a better way to work out "yesterday" then? Other than using "-d yesterday"?
  • Alnitak
    Alnitak over 12 years
    you're missing the backticks, which should run that command and put the output into the variable SOURCEFILE.
  • Alnitak
    Alnitak over 12 years
    @Dan it depends - what precise O/S are you running on?
  • Dan
    Dan over 12 years
    @Alnitak Just ran uname and apparently it's SunOS.
  • Dan
    Dan over 12 years
    D'oh! Tried it again using printf "DNXOUT-%04d%02d%02d.txt" $YEAR $MONTH $DAY and it worked! Thanks for your help.
  • jim mcnamara
    jim mcnamara over 12 years
    @dogbane - you need to be aware of the GNU date vs POSIX date issue when you give answers about using date. Otherwise your answer is fine.
  • jim mcnamara
    jim mcnamara over 12 years
    @Dan - GNU coreturils (Linux usually) has a date command that does date arithmetic. You do not have it. If you deliberately misuse the date command you can see what options date takes. Ex date -h where there is no -h option. And if there is one you still see what you can use for options.
  • itsbruce
    itsbruce over 11 years
    This added nothing than the existing, older answers.