shell: how to get the date before the current date ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tanyali
    New Member
    • Feb 2007
    • 43

    shell: how to get the date before the current date ?

    Hi guys,

    is there a function in shell( bash ) to get the date of the day before the current date ?

    I made it with a long script, because it becomes complicated with the beginning and the end of month and year, so I wonder is there a function in shell to get the date before the current date?
    who knows, please help !

    thanks a million.
    Tanya
  • prn
    Recognized Expert Contributor
    • Apr 2007
    • 254

    #2
    No. Bash has no built-in function to do that, but you can make your own.

    For example:
    Code:
    yesterday () {
    	i=`date --utc +%s`
    	j=$(($i-86400))
    	date --utc --date "1970-01-01 $j sec" "+%Y-%m-%d %T"
    }
    will return something like:
    2007-08-08 16:53:29

    If you want a different format, just change the format string (the last bit of the last line). See man date for how format strings work.

    You can put the function, however you might want to edit it, into your .bashrc if you want to use it on a regular basis. Or, of course, if you wanted it in a bash script, you can put it into that script.

    HTH,
    Paul

    Comment

    • prn
      Recognized Expert Contributor
      • Apr 2007
      • 254

      #3
      I noticed that my last function was doing something odd with the time. First off, it was trying to give back the time in UTC, which may (or may not) be appropriate for your needs. Furthermore, I was not even getting the right time in UTC, which I still don't understand, even after some attempt at debugging.

      However, with a little digging, I found that the whole thing is far more complicated than required as you can do this:
      Code:
      date -d "-1 day"
      which is far simpler and clearer, and saves a lot of silly debugging. :-)

      Enjoy,
      Paul

      Comment

      • tanyali
        New Member
        • Feb 2007
        • 43

        #4
        Originally posted by prn
        I noticed that my last function was doing something odd with the time. First off, it was trying to give back the time in UTC, which may (or may not) be appropriate for your needs. Furthermore, I was not even getting the right time in UTC, which I still don't understand, even after some attempt at debugging.

        However, with a little digging, I found that the whole thing is far more complicated than required as you can do this:
        Code:
        date -d "-1 day"
        which is far simpler and clearer, and saves a lot of silly debugging. :-)

        Enjoy,
        Paul
        thanks Paul, you saved me long long long script !
        I used it as : date +"%Y%m%d" -d "-1 day"
        and get 20070809.

        also in PHP, use the function mktime() to get any date, I used :

        $yesterday=mkti me(0, 0, 0, date("m") , date("d")-1, date("Y"));
        echo "yesterday is ".date("Ymd",$y esterday);

        thanks Paul .~_~.
        Tanya.

        Comment

        • Pelicano
          New Member
          • Dec 2007
          • 1

          #5
          Allmots Unix. The most simple way:
          TZ=GMT+24 date +"%Y/%M/%d"

          Comment

          • Gobold

            #6
            Instead of writing "-1 day", you can also use "1 day ago", which I find more intuitively. It's only different syntax and does absolutely the same.

            date -d "1 day ago"

            Comment

            Working...