Use the shell in Linux to get yesterday, tomorrow, or more days before the date

Source: Internet
Author: User
Tags current greenwich mean time set time

Use shell to get the date of yesterday, tomorrow or many days ago in linux

Time 2015-12-08 09:33:00 BlogJava-Expert Zone
Theme shell
Original address:

Use the shell to get the date of yesterday, tomorrow or many days before using linux:

In Linux, the man date -d parameter is ambiguous. The following examples further illustrate:

# -d, --date=STRING display time described by STRING, not `now’

[root@Gman root]# date -d next-day +%Y%m%d #tomorrow date
[root@Gman root]# date -d last-day +%Y%m%d # yesterday date
[root@Gman root]# date -d yesterday +%Y%m%d # yesterday date
[root@Gman root]# date -d tomorrow +%Y%m%d # tomorrow date
[root@Gman root]# date -d last-month +%Y%m #上月日期
[root@Gman root]# date -d next-month +%Y%m #Next month date
[root@Gman root]# date -d next-year +%Y #Next date
DATE=$(date +%Y%m%d --date ‘2 days ago‘) #Get the date yesterday or more days ago

Name : date

Access rights : All users

Usage: date [-u] [-d datestr] [-s datestr] [--utc] [--universal] [--date=datestr] [--set=datestr] [--help] [-- Version] [+FORMAT] [MMDDhhmm[[CC]YY][.ss]]

Description: date can be used to display or set the date and time of the system. In terms of display, the user can set the format to be displayed. The format is set to a plus sign followed by a number of tags. The list of available tags is as follows:

Time aspect:
% : printed
% %n : next line
%t : tab
%H : hour (00..23)
%I : hour (01..12)
%k : hour (0..23)
%l : hour (1..12)
%M : minutes (00..59)
%p : Display local AM or PM
%r : Direct display time (12-hour format, format hh:mm:ss [AP]M)
%s : From January 1, 1970 00:00:00 UTC The number of seconds so far %S : seconds (00..61)
%T : Direct display time (24-hour clock)
%X : equivalent to %H:%M:%S
%Z : display time zone

%a : Day of the week (Sun..Sat)
%A : Day of the week (Sunday..Saturday)
%b : month (Jan..Dec)
%B : month (January..December)
%c : Display date and time directly
%d : day (01..31)
%D : Display date directly (mm/dd/yy)
%h : same as %b
%j : the first few days of the year (001..366)
%m : month (01..12)
%U : The first few weeks of the year (00..53) (with Sunday as the first day of the week)
%w : the day of the week (0..6)
%W : The first few weeks of the year (00..53) (Monday is the first day of the week)
%x : Display date directly (mm/dd/yy)
%y : the last two digits of the year (00.99)
%Y : full year (0000..9999)

If you do not start with a plus sign, it means you want to set the time, and the time format is MMDDhhmm[[CC]YY][.ss],

Where MM is the month,

DD is the day,

Hh is hour,

Mm is minutes,

CC is the first two digits of the year,

YY is the two digits after the year,

Ss is the number of seconds


-d datestr : display the time set in datestr (non-system time)

--help : Display auxiliary messages

-s datestr : set the system time to the time set in datestr

-u : Show current Greenwich Mean Time

--version : display version number


Jump after displaying the time, then display the current date: date +%T%n%D

Show month and day : date +%B %d

Display date and set time (12:34:56) : date --date 12:34:56

Set the system current time (12:34:56):date --s 12:34:56

Note: When you don't want a meaningless 0 (say 1999/03/07), you can insert a - symbol in the tag, for example date +%-H:%-M:%-S will count the minutes and seconds The meaningless 0 is removed, like the original 08:09:04 will become 8:9:4. In addition, only the person who obtained the permission (such as root) can set the system time. After you change the system time as root, remember to write the system time to CMOS with clock -w so that the system time will continue to hold the latest correct value the next time you reboot.

Ntp time synchronization

The ntp service is installed by default under linux system. Manual ntp synchronization is as follows


Of course, you can also specify other ntp servers.

-------------------------------------------------- -----------------


The date tool does more work than just printing out the current system date. You can use it to get the date of the given day and get the relative date relative to the current date. Knowing that one day is the day of the week

Another extension of GNU's date command is the -d option, which is useful when you don't have a calendar on your desk (UNIX users don't need a calendar). Using this powerful option, provided by parameters that include dates as quotes, you can quickly find out exactly which day of the week a particular date is:

$ date -d "nov 22"

Wed Nov 22 00:00:00 EST 2006


In this example, you can see that November 22 this year is Wednesday.

So, assuming a major meeting on November 22, you can immediately see that this day is Wednesday, and that day you will be rushed to the resident office.

Get relative date

The d option also tells you which day of the day relative to the current date, several days or weeks from now, or before (past). This can be done by enclosing this relative offset in quotation marks as a parameter to the -d option.

For example, you need to know the date two weeks later. If you are at the shell prompt, you can get an answer quickly:

$ date -d ’2 weeks’

There are other important ways to use this command. With the next/last command, you can get the day of the week:
$ date -d ‘next monday’ (date of next Monday)
$ date -d next-day +%Y%m%d (date of tomorrow) or: date -d tomorrow +%Y%m%d
$ date -d last-day +%Y%m%d (date of yesterday) or: date -d yesterday +%Y%m%d
$ date -d last-month +%Y%m (last month is a few months)
$ date -d next-month +%Y%m (months in the next month)
With the ago command, you can get past dates:
$ date -d ‘30 days ago‘ (date of 30 days ago)
You can use negative numbers to get the opposite date:
$ date -d ‘dec 14 -2 weeks’ (relative: dec 14 date two weeks before this date)
$ date -d ‘-100 days‘ (dates from 100 days ago)
$ date -d ‘50 days‘ (date after 50 days)

This technique is very useful, it can set reminders for yourself based on future dates, perhaps in a script or shell startup file, as follows:

DAY=`date -d ‘2 weeks’ +"%b %d"`

If test "`echo $DAY`" = "Aug 16"; then echo ‘Product launch is now two weeks away!’; fi 

Use the shell in Linux to get yesterday, tomorrow, or more days before the date

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.