Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set and use date settings and date commands in Linux systems

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to set and use date setting and date commands in Linux system". In daily operation, I believe many people have doubts about how to set and use date settings and date commands in Linux system. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to set and use date settings and date commands in Linux system". Next, please follow the editor to study!

Name: date

Permissions: all users

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

Note: date can be used to display or set the date and time of the system. In terms of display, users can set the format they want to display. The format is set to a plus sign followed by several tags. The list of available tags is as follows:

In terms of time:

%: print out

% n: next line

% t: tabbed

% H: hours (00.23)

% I: hours (01.. 12)

% k: hour (0.23)

% l: hour (1.. 12)

% M: minutes (00.59)

% p: show local AM or PM

% r: direct display time (12-hour format in hh:mm:ss [AP] M)

% s: seconds since 00:00:00 UTC on January 1, 1970% S: seconds (00.61)

% T: direct display time (24-hour system)

% X: equivalent to% H:%M:%S

% Z: show time zone

In terms of date:

% a: what day of the week (Sun..Sat)

% A: what 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: direct display date (mm/dd/yy)

% h: same as% b

% j: day of the year (001. 366)

% m: month (01.. 12)

% U: week ordinal of the year (00.53) (with Sunday as the first day of the week)

% w: day of the week (0.6)

% W: week ordinal of the year (00.53) (with Monday as the first day of the week)

% x: direct display date (mm/dd/yy)

% y: last two digits of the year (00.99)

% Y: full year (000.9999)

If you do not start with a plus sign, you want to set the time, and the format of the time 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 last two digits of the year

Ss is number of seconds

Set up a plan:

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

-- help: displays auxiliary messages

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

-u: displays the current Greenwich mean time

-- version: displays the version number

Example:

Jump the line after the time is displayed, and then display the current date: date +% T%n%D

Show months and days: date +% B% d

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

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

Note: when you do not want meaningless zeros (such as 1999-03-07), you can insert a-symbol in the tag, such as date +%-H:%-M:%-S will remove the meaningless zeros in minutes and seconds, such as 08:09:04 will become 8:9:4. In addition, only those who have obtained the rights (such as root) can set the system time. When you change the system time as root, please remember to write the system time into CMOS with clock-w, so that the system time will keep the latest correct value the next time you reboot.

Ntp time synchronization

Ntp service is installed by default on linux system. Manual ntp synchronization is as follows

The code is as follows:

Ntpdate ntp1.nl.net

Of course, you can also specify other ntp servers

Extended function

The date tool can do more than just print out the current system date. You can use it to find out exactly what day of the week a given date is and to get a relative date relative to the current date. Know what day a certain day is

Another GNU extension to the 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, you can quickly find out what day of the week a particular date is by providing a parameter enclosed in quotation marks:

The code is as follows:

$date-d "nov 22"

Wed Nov 22 00:00:00 EST 2006

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

So, suppose a major meeting is held on November 22, and you will immediately know that it is Wednesday, and on that day you will arrive at the resident office.

Get relative date

The d option can also tell you what the number of days relative to the current date is, days or weeks from now, or before (in the past). You can do this by enclosing this relative offset in quotation marks as an argument to the-d option.

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

The code is as follows:

$date-d'2 weeks'

There are other important ways to use this command. Using the next/last directive, you can find out what day it will be in the future:

The code is as follows:

$date-d'next monday'

(date of next Monday)

The code is as follows:

$date-d next-day +% Y%m%d

(tomorrow's date) or:

The code is as follows:

Date-d tomorrow +% Y%m%d

The code is as follows:

$date-d last-day +% Y%m%d

(yesterday's date) or:

The code is as follows:

Date-d yesterday +% Y%m%d

The code is as follows:

$date-d last-month +% Y% m

(what month was last month)

The code is as follows:

$date-d next-month +% Y% m

(what month is next month)

Using the ago directive, you can get past dates:

The code is as follows:

$date-d'30 days ago'

(date 30 days ago)

You can use negative numbers to get the opposite date:

The code is as follows:

$date-d'dec 14-2 weeks'

(relative: date two weeks before the date of dec 14)

The code is as follows:

$date-d'- 100 days'

(date 100 days ago)

The code is as follows:

$date-d'50 days'

(date after 50 days)

This technique is very useful to set reminders for yourself based on a future date, perhaps in a script or Shell startup file, as follows:

The code is as follows:

DAY= `date-d'2 weeks' + "b d" `

If test "`Product launch is now two weeks $DAY`" = "Aug 16"; then echo 'Product launch is now two weeks awayhands; fi

At this point, the study on "how to set and use the date setting and date commands in the Linux system" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report