Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

In Linux, we run commands that execute immediately, so how do we delay and schedule execution? In the next few articles we will learn about time related commands, including date, at, sleep, crontab, etc. In this article we will start with the commonly used date.

First, basic use

The use of date is quite simple, the following command is the current system time.

$date
Copy the code

The following output is displayed:

Fri Sep 25 02:03:35 EDT 2021
Copy the code

The output is explained as follows:

  • An abbreviation for Fri – Friday
  • An abbreviation for Sep – September, for < September >
  • 25 – means the 25th, so the current date is September 25th
  • 02:03:35. – That means 2:3:35
  • EDT – Indicates daylight Saving time in the United States, 12 hours later than Beijing time
  • 2021 – Indicates the year 2021

The time format for printing is somewhat complicated and difficult to understand. We can also customize the output format.

Second, customize the output format

With custom output formats, we can use the command man date reference manual to have selective and formatted output content.

Gets the number of minutes at the current moment

$date "+%M"
Copy the code

The output of the control is

23
Copy the code

Customize different styles of minute and second formats

date "+%H:%M:%S"
Copy the code

Console output

02:31:22
Copy the code

Custom Chinese delimiters are also available

Date "+%H %M minute %S seconds"Copy the code

Console output

2:31:10 secondsCopy the code

Isn’t that interesting? Is there a pattern? Let me briefly summarize the gameplay:

  • The customdateCommand output+“, followed by other symbols to indicate the different parts of the custom, as much as possible between double quotation marks.
  • %H %M %S is an abbreviation for hours, minutes and seconds. By extension, %Y represents years, and so on.
  • Custom delimiters can be used to distinguish data.
  • Please use it if you don’t understandman date.

Three, extension,

If you think that’s all date can do, you’re underestimating it. It can even be used to change the system time.

$sudo date 10101010
Copy the code

As user root, the change time is 10:10 on October 10. The year and the number of seconds are not specified, so the two will not be changed.

Today’s article is about the time definition of the moment, the next article is about the at delay execution command.