How to Use the date command in Linux

Source: Internet
Author: User
Tags vps hosting

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:

  1. $ date
  2. Sat2Dec12:34:12 CST 2017

 

Syntax
  1. Usage:date[OPTION]...[+FORMAT]
  2. or:date[-u|--utc|--universal][MMDDhhmm[[CC]YY][.ss]]
  3. 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
  1. date-d "5 weeks"
  2. SunJan719:53:50 CST 2018

 

2. Search for the date after 5 weeks and after 4 days
  1. date-d "5 weeks 4 days"
  2. ThuJan1119:55:35 CST 2018

 

3. Get the Date of next month
  1. date-d "next month"
  2. WedJan319:57:43 CST 2018

 

4. Obtain the date of the next Sunday
  1. date-d last-sunday
  2. 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
  1. date+"%F"
  2. 2017-12-03

 

6. mm/dd/yyyyFormat display date
  1. date+"%m/%d/%Y"
  2. 12/03/2017

 

7. Only display time
  1. date+"%T"
  2. 20:07:04

 

8. Show that today is the day of the year
  1. date+"%j"
  2. 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.

  1. 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

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: info-contact@alibabacloud.com 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.