Get the date (a day before current time) in Bash
For the usage on Ubuntu with date function: $(date +%Y:%m:%d -d «1 day ago»), output is: 2013:04:21. And if you want the date 10 days before, you can use $(date +%Y:%m:%d -d «10 days ago») or $(date +%Y:%m:%d -d «10 day ago»)
This bash function will work on both Linux and OSX. Basically it tries the GNU Linux style first. If that fails it tries the OSX style. Call it with an argument of the number of days in the past you want the date. If you pass no argument it assume 0 days. This code is a one-liner, so cut and paste should work — no line endings assumed. date_days_past () < days_past=$<1:-0>; if ! date -v-$d +%Y%m%d 2>/dev/null; then date —date=»-$ day» +%Y%m%d; fi >1:-0>
If you have BSD (OSX) date you can do it like this:
date -j -v-1d Wed Dec 14 15:34:14 CET 2011
Or if you want to do date calculations on an arbitrary date:
date -j -v-1d -f "%Y-%m-%d" "2011-09-01" "+%Y-%m-%d" 2011-08-31
You need to install the GNU version of date , available through the coreutils package, to make this syntax work on macOS. If you’re using Homebrew, you can install it via brew install coreutils , after which you should be able to use GNU date through the gdate command. See: topbug.net/blog/2013/04/14/…
where 1d defines current day minus 1 day. Similarly,
date -v-1w +%F — for previous week date
date -v-1m +%F — for previous month date
IF YOU HAVE GNU DATE,
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
This method is not 100% reliable (about 98% actually) if you happen to live in a place using daylight saving time.
Well this is a late answer,but this seems to work!!
YESTERDAY=`TZ=GMT+24 date +%d-%m-%Y`; echo $YESTERDAY;
For details about the date format see the man page for date
date=$(date -d "yesterday" '+%Y-%m-%d') echo $date
That results in date: illegal option: -d on Solaris. Your answer that relies on non-portable GNU extensions to the POSIX-standard date utility for a question about the non-GNU Solaris platform.
perl -e 'print scalar localtime( time - 86400 ) . "\n";'
Or, use nawk and (ab)use /usr/bin/adb:
/usr/bin/truss /usr/bin/date 2>&1 | nawk -F= '/^time\(\)/ ' | adb
Not very sexy but might do the job:
perl -e 'my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time - 86400);$year += 1900; $mon+= 1; printf ("YESTERDAY: %04d%02d%02d \n", $year, $mon, $mday)'
Formated from «martin clayton» answer.
You could do a simple calculation, pimped with an regex, if the chosen date format is ‘YYYYMM’:
echo $(($(date +"%Y%m") - 1)) | sed -e 's/99$/12/'
In January of 2020 it will return 201912 😉 But, it’s only a workaround, when date does not have calculation options and other dateinterpreter options (e.g. using perl) not available 😉
if you are using OSX, but you need create for GNU compatible, install coreutils first
then edit your profile with:
#gnu coreutils first export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
re-start your terminal, and now you able to use GNU format!
Puts yesterday’s date in YYYY-MM-DD format into variable $yesterday .
#!/bin/bash OFFSET=1; eval `date "+day=%d; month=%m; year=%Y"` # Subtract offset from day, if it goes below one use 'cal' # to determine the number of days in the previous month. day=`expr $day - $OFFSET` if [ $day -le 0 ] ;then month=`expr $month - 1` if [ $month -eq 0 ] ;then year=`expr $year - 1` month=12 fi set `cal $month $year` xday=$ day=`expr $xday + $day` fi echo $year-$month-$day
Hi medoix, I am not able to find the meaning of $. I know that $# stands for the number of arguments to a script/function. But I am not able to relate to this. Please help.$#>
This doesn’t work for the previus day on change of month . For example today is 1 July , 2013 , the result being printed out is 2013-6-1347 , whereas the expected result is 2013-6-30
Manipulating the Timezone is possible for changing the clock some hours. Due to the daylight saving time, 24 hours ago can be today or the day before yesterday.
You are sure that yesterday is 20 or 30 hours ago. Which one? Well, the most recent one that is not today.
echo -e "$(TZ=GMT+30 date +%Y-%m-%d)\n$(TZ=GMT+20 date +%Y-%m-%d)" | grep -v $(date +%Y-%m-%d) | tail -1
The -e parameter used in the echo command is needed with bash, but will not work with ksh. In ksh you can use the same command without the -e flag.
When your script will be used in different environments, you can start the script with #!/bin/ksh or #!/bin/bash. You could also replace the \n by a newline:
echo "$(TZ=GMT+30 date +%Y-%m-%d) $(TZ=GMT+20 date +%Y-%m-%d)" | grep -v $(date +%Y-%m-%d) | tail -1
Today’s date, minus X days in shell script
I need to create three variables, each for Year, Month, and Day for Today’s date, minus X number of days. For this question I’ll choose a random amount of days: 222. So if:
TodayYear=`date +%Y` TodayMonth=`date +%m` TodayDay=`date +%d`
222days_before_TodayYear=. 222days_before_TodayMonth=. 222days_before_TodayDay=.
5 Answers 5
For GNU date :
date_222days_before_TodayYear=$(date --date="222 days ago" +"%Y") date_222days_before_TodayMonth=$(date --date="222 days ago" +"%m") date_222days_before_TodayDay=$(date --date="222 days ago" +"%d")
For BSD date ::
If you are using OS X or FreeBSD, use the following instead because BSD date is different from GNU date:
date_222days_before_TodayYear=$(date -j -v-222d +"%Y") date_222days_before_TodayMonth=$(date -j -v-222d +"%m") date_222days_before_TodayDay=$(date -j -v-222d +"%d")
In bash and many other languages, you cannot start a variable name with a numerical character, so I prefixed them with date_ for you.
Second Update: New requirement — Using 222 Working days instead of 222 Regular days:
(Assumption: Not considering statutory holidays, because that just gets far beyond the scope of what I can help you with in a shell script:)
Consider 222 working days:
- 5 working days per week, that is floor(222/5) == 44 weeks
- 44 weeks * 7 days per week == 308 days
- Extra days leftover: 222 % 5 == 2
- Therefore 222 working days == 310 regular days
But, there is a catch! If the number of regular days is 308 or some multiple of 7 , then we would have been fine, because any multiple of 7-days ago from a working day is still a working day. So we need to consider whether today is a Monday or a Tuesday:
- If today is a Monday, we’d get Saturday where we wanted Thursday
- If today is a Tuesday, we’d get Sunday where we wanted Friday
So you see we need an additional offset of 2 more days if today is either Monday or Tuesday; so let’s find that out first before we proceed:
#!/bin/bash # Use 310 days as offset instead of 222 offset=310 # Find locale's abbreviated weekday name (e.g., Sun) today=$(date -j +"%a") # Check for Mon/Tue if [[ "$today" == "Mon" ]] || [[ "$today" == "Tue" ]]; then offset=$((offset+2)) fi date_222_working_days_before_TodayYear=$(date -j -v-$d +"%Y") date_222_working_days_before_TodayMonth=$(date -j -v-$d +"%m") date_222_working_days_before_TodayDay=$(date -j -v-$d +"%d")
Subtract days from a date in Bash
You are specifying the date incorrectly. Instead, say:
If you need to store it in a variable, use $(. ) :
p_dataset_date=$(date --date="$ -$ day" +%Y-%m-%d)
I had to modify this to $ date «—date=$
@x_mtd Yes, you need to set the variable date_diff . Set it to the number of days that you want to subtract.
Very slight improvement to the command — date —date=»$
yesterday=$(date -d "$date -1 days" +"%Y%m%d")
thank you.. I just needed in seconds format, hence went with thirtydaysold=$(date -d «$date -30 days» ‘+%s’)
If you’re not on linux, maybe mac or somewhere else, this wont work. you could check with this:
to get more details, you could also see
To me, it makes more sense if I put the options outside (easier to group), in case I will want more of them.
date -d "$dataset_date - $date_diff days" +%Y-%m-%d
1. -d --------------------------------- options, in this case followed need to be date in string format (look up on $ man date) 2. "$dataset_date - $date_diff days" -- date arithmetic, more have a look at article by [PETER LEUNG][1] 3. +%Y-%m-%d -------------------------- your desired format, year-month-day
Shell Script with wget to get date minus 1-day
The tricky part is that the document I want to load (2012_04_02_data.xls) is dated for the previous day. So I need to download the previous days data, not the current day. My goal is to run the shell script without having to increment the calendar day each morning. Instead, the shell will take the current day and subtract one calendar day and then load that date within the url.
4 Answers 4
wget "http://www.mywebpage.com/data/reports/$(date -d yesterday +%Y_%m_%d)_data.xls"
This ought to work for GNU coreutils, which based on the linux tag I’m guessing you have.
You can use the date command
$ wget "http://www.mywebpage.com/data/reports/$(date -d yesterday +'%Y_%m_%d')_data.xls"
The more tricky part is, if you have to handle off days. Usually I wget the link containing page and then grep the date out of the href attribute. then pass the string to your «real» wget command.
You tagged this with perl , so here’s how I’d do it with Perl. That doesn’t mean I think you should use Perl though:
use v5.10.1; use DateTime; use Mojo::UserAgent; chdir 'Desktop/Reports/folder' or die "Could not chdir: $!"; my $date = DateTime->now->subtract( days => 1 )->ymd( '_' ); my $url = sprintf "http://www.mywebpage.com/data/reports/%s_data.xls", $date; my $data = Mojo::UserAgent->new->get( $url )->res->body;
You can do more fancy stuff with the user agent to save chunks to a file as it comes in and many other things you might want to do, but you’ll have to fill in those bits yourself.