subtract days from a date in bash

前端 未结 6 1673
梦谈多话
梦谈多话 2020-12-13 23:13

I want to subtract \"number of days\" from a date in bash. I am trying something like this ..

echo $dataset_date #output is 2013-08-07

echo $date_diff #outp         


        
6条回答
  •  甜味超标
    2020-12-13 23:58

    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
    

    Where:

     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
    

提交回复
热议问题