How to Use the date command in Linux
In this article, we will use some cases to demonstrate how to usedateCommand.dateCommand to output/set the system date and time.dateThe command is simple. See the example and syntax below.
By defaultdateCommand, it will output the current system Date and Time:
$ date
Sat2Dec12:34:12 CST 2017
Syntax
Usage:date[OPTION]...[+FORMAT]
or:date[-u|--utc|--universal][MMDDhhmm[[CC]YY][.ss]]
Display the current time in a given format or set the system time.
Case
The following cases will show you how to usedateCommand to view the date and time before and after a period of time.
1. Search for the date after 5 weeks
date-d "5 weeks"
SunJan719:53:50 CST 2018
2. Search for the date after 5 weeks and after 4 days
date-d "5 weeks 4 days"
ThuJan1119:55:35 CST 2018
3. Get the Date of next month
date-d "next month"
WedJan319:57:43 CST 2018
4. Obtain the date of the next Sunday
date-d last-sunday
SunNov2600:00:00 CST 2017
dateThe command also has many formatting options. The following example shows how to formatdateCommand output.
5.
yyyy-mm-ddFormat display date
date+"%F"
2017-12-03
6.
mm/dd/yyyyFormat display date
date+"%m/%d/%Y"
12/03/2017
7. Only display time
date+"%T"
20:07:04
8. Show that today is the day of the year
date+"%j"
337
9. Formatting-related options
<If the display is incomplete, Slide left and right>
| Format |
Description |
%% |
Display percentage (%). |
%a |
Abbreviation of a week (for example:Sun). |
%A |
The complete form of the week (for example:Sunday). |
%b |
Abbreviated month (for example:Jan). |
%B |
The full name of the month in the current region (for example:January). |
%c |
Date and time (for example:Thu Mar 3 23:05:25 2005). |
%C |
Current century; similar%YBut the last two digits (for example:20). |
%d |
The day of the month (for example:01). |
%D |
Date; effect and%m/%d/%ySame. |
%e |
The day of the month, which is filled with spaces.%_dSame. |
%F |
Complete date; followed%Y-%m-%dSame. |
%g |
The last two digits of the year (see%G). |
%G |
Year (see%V);%V. |
%h |
Same%b. |
%H |
Hour (00..23). |
%I |
Hour (01..12). |
%j |
The day of the year (001..366). |
%k |
Hour, filled with spaces (0..23); And%_HSame. |
%l |
Hour, filled with spaces (1..12); And%_ISame. |
%m |
Month (01..12). |
%M |
Minutes (00..59). |
%n |
Line feed. |
%N |
Nanoseconds (000000000..999999999). |
%p |
The current region is in the morning.AMAfternoonPM; Null if unknown. |
%P |
Similar%p. |
%r |
The 12-hour display time of the Current region (for example:11:11:04 PM). |
%R |
The hour and minute in the 24-hour format.%H:%M. |
%s |
The number of seconds that have elapsed since 00:00:00 UTC. |
%S |
Number of seconds (00..60). |
%t |
Tab. |
%T |
Time; same%H:%M:%S. |
%u |
Week (1..7); 1 indicatesMonday. |
%U |
The week of the year, starting from Sunday (00..53). |
%V |
The week of the year, starting from Monday (01..53). |
%w |
Use a number to indicate the number of weeks (0..6); 0 indicatesSunday. |
%W |
The week of the year, Monday is the beginning of a week (00..53). |
%x |
The date representation of the current region (for example:12/31/99). |
%X |
Time Representation of the current region (for example:23:13:48). |
%y |
The last two digits of the Year (00..99). |
%Y |
Year. |
%z |
To+hhmmThe numeric format of Time Zone (for example:-0400). |
%:z |
To+hh:mmThe numeric format of Time Zone (for example:-04:00). |
%::z |
To+hh:mm:ssThe numeric format of Time Zone (for example:-04:00:00). |
%:::z |
The time zone is represented in numeric format.:The number is determined by the precision you need (for example,-04,+05:30). |
%Z |
Abbreviation of the time zone (for example,EDT). |
10. Set the system time
You can also usedateTo manually set the system time by using--setThe following example sets the system time to August 30, 2017 04:22 P.M.
date--set="20170830 16:22"
Of course, if you are using our VPS hosting service, you can always contact and consult our Linux expert Administrator (by calling customer service or submitting a ticket) aboutdateCommand. They are 24 × 7 online and will help you immediately. (LCTT: the original advertisement ~)
PS. If you like this post, click the button below to share or leave a message. Thank you.
Via: https://www.rosehosting.com/blog/use-the-date-command-in-linux/
Author: rosehosting Translator: lujun9972 Proofreader: wxy
This article was originally compiled by LCTT and launched with the honor of Linux in China