Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, January 12, 2016

How to increment a date in a bash script

How to increment a date in a bash script


I have a bash script that takes an argument of a date (yyyy-mm-dd)

I convert it to seconds with

startdate="$(date -d"$1" +%s)";  

What I need to do is iterate 8 times, each time incrementing the epoch date by 1 day and then displaying it in the format mm-dd-yyyy

Answer by Aleks-Daniel Jakimenko for How to increment a date in a bash script


startdate=$(date -d"$1" +%s)  next=86400 # 86400 is one day    for (( i=startdate; i < startdate + 8*next; i+=next )); do       date -d"@$i" +%d-%m-%Y  done  

Answer by ceving for How to increment a date in a bash script


It is not that easy to increment days. Normally it is done by converting the Gregorian date into a Julian day number. Then you can increment the day. And after that you calculate the Gregorian date. Here is example code:

http://it.toolbox.com/wiki/index.php/Convert_a_date_to_a_Julian_day

http://it.toolbox.com/wiki/index.php/Convert_a_Julian_day_to_a_date

Answer by swdev for How to increment a date in a bash script


Use the date command's ability to add days to existing dates.

The following:

DATE=2013-05-25    for i in {0..8}  do     NEXT_DATE=$(date +%m-%d-%Y -d "$DATE + $i day")     echo "$NEXT_DATE"  done  

produces:

05-25-2013  05-26-2013  05-27-2013  05-28-2013  05-29-2013  05-30-2013  05-31-2013  06-01-2013  06-02-2013  

Note, this works well in your case but other date formats such as yyyymmdd may need to include "UTC" in the date string (eg. date -ud "20130515 UTC + 1 day")

Answer by Bill Matsoukas for How to increment a date in a bash script


Just another way to increment or decrement days from today that's a bit more compact:

$ date %y%m%d ## show the current date  $ 20150109  $ ## add a day:  $ echo $(date %y%m%d -d "$(date) + 1 day")  $ 20150110  $ ## Subtract a day:  $ echo $(date %y%m%d -d "$(date) - 1 day")  $ 20150108  $   

Answer by Benoy G for How to increment a date in a bash script


Increment date in bash script and create folder structure based on Year, Month and Date to organize the large number of files from a command line output.

for m in {0..100}  do      folderdt=$(date -d "Aug 1 2014 + $m days" +'%Y/%m/%d')      procdate=$(date -d "Aug 1 2014 + $m days" +'%Y.%m.%d')      echo $folderdt      mkdir -p $folderdt      #chown : $folderdt -R      cd $folderdt      #commandline --process-date $procdate      cd -  done  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.