How to get the date a few days ago or a few days later in linux

It is used more in shell scripts:

In the linux environment to get the period a few days ago just use

date -d "x days ago" +%Y%m%d

Replace x with a number. If you need a positive number a few days ago , you can directly write a negative number if you want a few days later ;

date -d "x weeks ago" +%Y%m%d

Replace x with a number. If you need to write a positive number a few weeks ago , you can directly write a negative number if you want a date a few weeks later ;

date -d "x years ago" +%Y%m%d

Replace x with a number. If you need to write a positive number a few years ago , you can directly write a negative number if you want a date a few years later ;

See the following example:

[root@showsky ~]# date +%Y%m%d
20100107

Above is today's date 20100107

[root@showsky ~]# date -d "2 days ago" +%Y%m%d         
20100105

 

The above is the date two days ago
[root@showsky ~]#  date -d "4 days ago" +%Y%m%d 
20100103

The above is the date four days ago

[root@showsky ~]#  date -d "-1 days ago" +%Y%m%d
20100108 The
above is the date one day later
[root@showsky ~]#  date -d "-2 days ago" +%Y%m%d 
20100109
The above is the date two days ago

[root@showsky ~]#  date -d "1 week ago" +%Y%m%d      
20091231 The
above is the date one week ago
[root@showsky ~]#  date -d "1 year ago" +%Y%m% d    
20090107
above is the date one year ago

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326995681&siteId=291194637