In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to use the date command in Linux". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn how to use the date command in Linux.
Date is a system command included in the Linux system, which is used to display the current system time. The Linux date command 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.
Linux date command specific use method date command
The function of the date command is to display and set the system date and time.
The general format of the command is: date [option] display time format (begins with +, followed by format)
Date sets the time format
The meanings of the options in the command are:
-d datestr,-date datestr displays the date described by datestr
-s datestr,-set datestr sets the date described by datestr
-u,-universal displays or sets the universal time domain
% H hours (00.23)
% I hours (01.. 12)
% k hours (0.23)
% l hours (1.. 12)
% M points (00.59)
% p shows AM or PM
% r time (hh:mm:ss AM or PM), 12 hours
% s seconds experienced since 00:00:00 on January 1, 1970
% s seconds (00.59)
% T time (24-hour) (hh:mm:ss)
Format of X display time (H:%M:%S)
% Z time zone date field
% an abbreviation of day of the week (Sun..Sat)
The full name of% A day of the week (Sunday..Saturday)
Abbreviation for% b month (Jan..Dec)
Full name of% B month (January..December)
C date and time (Mon Nov 8 14:12:46 CST 1999)
% d the day ordinal of a month (01.. 31)
% D date (mm/dd/yy)
H and b options are the same
% j the day ordinal of a year (001. 366)
% m month (01.. 12)
% w the day of the week (0 represents Sunday)
% W the week ordinal of the year (00.53, Monday is the first day)
% x format of display date (mm/dd/yy)
The last two numbers of% y year (1999 is 99)
% Y year (for example, 1970, 1996, etc.)
It is important to note that only superusers can set the time with the date command, and the average user can only use the date command to display the time.
The following environment variables will affect the execution of the date command.
LANG determines the default locale to be used if neither LC_ALL nor the corresponding environment variable (starting with LC_) specifies the locale.
LC_ALL determines the locale used to override any locale category value set by LANG or any environment variable that begins with LC_.
The locale in which LC_CTYPE determines that the byte sequence of text data is interpreted as characters (for example, a single pair of multibyte characters in a parameter).
LC_MESSAGES decides the language in which the information should be written.
LC_TIME determines the contents of the date and time string written by date.
NLSPATH determines the location of the message directory for processing LC_MESSAGES.
TZ specifies the time zone to use for the time and date, unless the-u option is specified. If the TZ variable is not set and the-u flag is not specified, the non-specified system default time zone is used.
Use example 1:
# date / / Show current date
# date-s / / sets the current time. Only root permission can be set, and others can only be viewed.
# date-s 20061010 / / is set to 20061010, which sets the specific time to empty 00:00:00
# date-s 12:23:23 / / set the specific time and will not change the date
# date-s "12:12:23 2006-10-10" / / this sets the full time
CST: China Standard time (China Standard Time), this interpretation may be aimed at RedHat Linux.
UTC: coordinated Universal time, also known as Universal Standard time, or UTC for short, comes from English International time / French coordinated time "Universal Time/Temps Cordonné". The time difference between time and UTC in Chinese mainland, Hong Kong, Macau, Taiwan, Mongolia, Singapore, Malaysia, Philippines and Western Australia is + 8, that is, UTC+8.
GMT: Greenwich mean time (Greenwich mean time or Greenwich Mean Time,GMT) is the standard time at the Royal Greenwich Observatory on the outskirts of London, England, because the prime meridian is defined as the longitude through which it passes.
After setting the system time, you also need to synchronize to the hardware clock # clock-systohc
The hardware clock synchronizes with the system clock:
# [wc] (http://www.linuxso.com/command/hlock.html 'target='_blank' > hwclock-hctosys or # clock-hctosys
In the above command,-hctosys stands for Hardware Clock to SYStem clock.
Synchronize the system clock with the hardware clock:
# hwclock-systohc or # clock-systohc
Use example 2:
Displays the time in the specified format.
$date & lsquo;+This date now is = >% x, time is now = >% X, thank you!'
This date now is = > 11-12-99, time is now = > 17:53:01, thank you!
Displays the current time in a predetermined format.
# date
Fri Nov 26 15:20:18 CST 1999
The time is set for 14:36 in the afternoon.
# date-s 14:36:00
Fri Nov 26 14:15:00 CST 1999
The setting time is November 28, 1999.
# date-s 991128
Sun Nov 28 00:00:00 CST 1999
Set up a day ago
Date-date "1 days ago" + "Y-%m-%d"
Tips on Date command parameters
Because Linux says vaguely about the man date-d parameter, the examples are as follows:
#-d,-date=STRING display time described by STRING, not `now'
For Linux
[root@linuxso.com ~] # date-d next-day +% Y%m%d
20060328
[root@linuxso.com ~] # date-d last-day +% Y%m%d
20060326
[root@linuxso.com ~] # date-d yesterday +% Y%m%d
20060326
[root@linuxso.com ~] # date-d tomorrow +% Y%m%d
20060328
[root@linuxso.com ~] # date-d last-month +% Y% m
200602
[root@linuxso.com ~] # date-d next-month +% Y% m
200604
[root@linuxso.com ~] # date-d next-year +% Y
2007
FreeBSD is different; examples are as follows:
For FreeBSD
[root@linuxso.com] # date-v-1D +% Y%m%d
20060326
[root@linuxso.com] # date-v-1m +% Y%m%d
20060227
[root@linuxso.com] # date-v-1y +% Y%m%d
20050327
Use example 3:
In the linux environment, to get the period a few days ago, just use the
Date-d "x days ago" +% Y%m%d
X is replaced by a number. If you need to write a positive number directly from a few days ago, you can write a negative number directly if you want the date a few days later.
Date-d "x weeks ago" +% Y%m%d
X is replaced by a number. If you need to write a positive number directly from a few weeks ago, you can write a negative number directly if you want a date a few weeks later.
Date-d "x years ago" +% Y%m%d
X is replaced by a number. If you need to write a positive number directly from a few years ago, you can write a negative number directly if you want the date a few years later.
Look at the following example:
[root@linuxso.com ~] # date +% Y%m%d
20100107
It's today's date 20100107.
[root@linuxso.com ~] # date-d "2 days ago" +% Y%m%d
20100105
It's the date from two days ago.
[root@linuxso.com ~] # date-d "4 days ago" +% Y%m%d
20100103
It's the date from four days ago.
[root@linuxso.com ~] # date-d "- 1 days ago" +% Y%m%d
20100108
It's the date a day later.
[root@linuxso.com ~] # date-d "- 2 days ago" +% Y%m%d
20100109
It's the date two days later.
[root@linuxso.com ~] # date-d "1 week ago" +% Y%m%d
20091231
It's the date of a week ago.
[root@linuxso.com ~] # date-d "1 year ago" +% Y%m%d
20090107
It's the date of a year ago.
The above is all the contents of the article "how to use date commands in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.