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 use the date command under Linux

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Editor to share with you how to use the date command under Linux, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Display time is a common command, and date-related file names or time displays are often used in writing shell scripts. Both Linux and windows are date commands.

Usage of date command under Linux

Date [OPTION]... [+ FORMAT]

Date [- u |-- utc |-- universal] [MMDDhhmm [[CC] YY] [.ss]]

Date command parameters

-d,-date=STRING displays the time specified by STRING

-f,-file=DATEFILE similar-the date parameter displays each line of time in the DATEFILE file

-ITIMESPEC,-iso-8601 [= TIMESPEC] displays the date / time in ISO 8601 format. TIMESPEC is one of "date" (showing date only), "hours", "minutes", "senconds" (showing time precision), and defaults to "date".

-r,-reference=FILE displays the last modification time of the file

-R,-rfc-2822 displays the time in RFC-2822 compatible date format

-s,-set=STRING set time to STRING

-u,-utc,-universal display or set to Coordinated Universal Time time format

Date command output display format

% character%

Abbreviation for% a week (Sun..Sat)

Full name of% A week (Sunday..Saturday)

Abbreviation for% b month (Jan..Dec)

Full name of month B (January..December)

% c date time (Sat Nov 04 12:02:33 EST 1989)

% C Century (years divided by 100) [00-99]

% d the day ordinal of a month (01.. 31)

% D date (mm/dd/yy)

% e the day ordinal of a month (1. 31)

% F date, same as% Y-%m-%d

% g year (yy)

% G year (yyyy)

% h same as% b

% H hours (00.23)

% I hours (01.. 12)

% j the day ordinal of a year (001. 366)

% k hours (0.23)

% l hours (1.. 12)

% m month (01.12)

% M minutes (00.59)

% n line feeds

% N nanosecond (0000000.999999999)

% p AM or PM

% P am or pm

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

% R 24-hour time (hh:mm)

S seconds from 00:00:00 1970-01-01 UTC

% s seconds (00.60)

% t tab

% T 24-hour time (hh:mm:ss)

% u the day of the week (1. 7); 1 means Monday

% U the week ordinal of the year, Sunday is the first day of the week (00.53)

% V the week ordinal of the year, Monday is the first day of the week (01. 53)

% w the day of the week (0.6); 0 represents Sunday

% W the week ordinal of the year, Monday is the first day of the week (00.53)

% x date (mm/dd/yy)

X time (H:%M:%S)

% y year (00.99)

% Y year (1970 …)

Z RFC-2822 style number format time zone (- 0500)

Z time zone (e.g., EDT), empty if it cannot be determined

Here are some experiments done to make it easy to understand

$date-d "2010-11-15 23:00:01" Mon Nov 15 23:00:01 PST 2010$ date-d "2010-11-15 23:0:2" Mon Nov 15 23:00:02 PST 2010$ date-d "2010/11/15T23:0:2" Mon Nov 15 08:00:02 PST 2010$ echo "2010-11-15 23:00:01" > date.txt$ echo "2010-11-15 2300 date 02" > date.txt$ cat date.txt2010-11-15 23:00 : 01201011 date.txtMon Nov 15 23:00:02$ date-f date.txtMon Nov 15 23:00:01 PST 2010Mon Nov 15 23:00:02 PST 2010$ ls-ltotal 4When rwkashi rkashi-1 znan sybase 40 Nov 15 21:14 date.txt$ date-r date.txtMon Nov 15 21:14:36 PST 2010$ date-I2010-11-15$ date-Ihours2010-11-15T21-0800$ date-Iminutes2010-11-15T21:16-0800$ date-Iseconds2010-11-15T21:16:24-0800$ date RMon- 15 Nov 2010 21:47:08-0800$ date-uTue Nov 16 05:47:13 UTC 2010$ date + "Today is A." Today is Monday.$ date + "Date:%b.% e,% G" Date:Nov. 15, 2010$ date + "Date:% b.% e,% G" Date: Nov.15 2010$ date + "% x% X" 11 PM$ date PM$ date + "% Y-%m-%d% H:%M:%S" 2010-11-15 21:51:32$ date + "% Y-%m-%d% I:%M:%S% p" 2010-11-15 09:51:55 PM

Linux date common time format

Date=$ (date "+% Y-%m-%d___%H:%M:%S")

Echo date

2012-08-16 May 05VOV 52RV 20

Date "+% Y-%m-%d"

2012-08-16

-

Linux date date addition and subtraction operation

Date / / default time format

Thu Aug 16 05:42:38 UTC 2012

Date + "b% e,% G" / / Custom format

Aug 16, 2012

Date + "% b% e,% G"-dazzle day' or date-dazzle day' + "% b% e,% G" / / minus one day (similar to plus one day)

Aug 15, 2012

Date + "b% e,% G"-dudes 1 month' or date-dudes 1 month' + "b% e,% G" / / plus January (similar to minus one month)

Sep 16, 2012

Date + "Y year m month d day"

August 20, 2012

Date + "% Y% m / d% d"-dumbmur1 day' / / same as above plus or minus 1 day (or plus or minus 1 month)

August 19, 2012

Date + "% Y year%-m month% d day" / /%-m remove the zero of the month to it

August 20, 2012

-

Ubuntu modifies system time

Sudo date-s MM/DD/YY

Sudo date-s hh:mm:ss

Note that we are talking about system time, which is maintained by linux by the operating system.

When the system starts, the Linux operating system reads the time from CMOS to the system time variable, and the later modification time is realized by modifying the system time. In order to maintain the consistency between system time and CMOS time, Linux writes system time to CMOS at regular intervals. Since the synchronization takes place at regular intervals (about 11 minutes), if we restart the machine immediately after we execute date-s, the modification time may not be written to CMOS, which is the cause of the problem.

If you want to ensure that the modified hardware time takes effect, you can execute the following command.

Sudo dwclock-w or sudo clock-w (sometimes clock-w cannot be used under ubuntu without this command, if not, use this hwclock-w)

This command forces the system time to be written to CMOS.

View hardware time

Sudo hwclock-show

$sudo hwclock-show

Thursday, August 16, 2012 PM04:46:32 UTC-0.664019 seconds

Core tip: synchronize the ubuntu10.10 time on the VMware virtual machine with the network

Each time the virtual machine is started, the time for ubuntu10.10 is not appropriate, and it is found that this setting is needed:

System-> system Administration-> time and date

Time zone selection: Asia/Chongqing

Configuration options: keep in sync with the Internet server (when prompted to install software named ntp, install it, prompt to replace a file after installation, select "replace")

Time server selection: time.nuri.NET (Korea,Asia)

View file commands: ls-l and ls-l-- time-style=long-iso

The above is all the contents of this article entitled "how to use date commands under 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.

Share To

Servers

Wechat

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

12
Report