Shell time (date) related commands


date +%F
date -d last-day +%Y-%m-%d
date -d yesterday +%Y-%m-%d
date -d next-day +%Y-%m-%d  
date -d tomorrow +%Y-%m-%d
date -d '2 days ago' +%Y-%m-%d
date -d '2 weeks ago' +%Y-%m-%d
date -d '2 months ago' +%Y-%m-%d
date -d '2 years ago' +%Y-%m-%d
date -d '2 days' +%Y-%m-%d
date -d '2 weeks' +%Y-%m-%d
date -d '2 months' +%Y-%m-%d
date -d '2 years' +%Y-%m-%d
date -d last-month +%Y-%m-%d
date -d next-month +%Y-%m-%d
date -d next-year +%Y-%m-%d
date +%Y-%m-%d-%H-%M
date +%Y-%-m-%d-%-H-%-M

Date command and system time setting in Linux

Linux clock is divided into system clock and real time clock (RTC). The system clock refers to the clock in the current Linux kernel, while the hardware clock is the battery powered clock on the motherboard. This hardware clock can be set in BIOS. When Linux starts, the hardware clock will read the settings of the system clock, and then the system clock will operate independently of the hardware.
All commands (including functions) in Linux are set by the system clock. In Linux, the commands for clock viewing and setting mainly include date and hwlock.
Name: Date
Permission: all users
date [-u] [-d datestr] [-s datestr] [–utc] [–universal] [–date=datestr] [–set=datestr] [–help] [–version] [+FORMAT] [MMDDhhmm[[CC]YY][.ss]]
Date can be used to display or set the date and time of the system. In terms of display, the user can set the format to be displayed. 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: skip
%H: hours (00-23)
%I: hours (01-12)
%K: hours (0-23)
%L: hours (1-12)
%M: minutes (00-59)
%P: display local am or PM
%R: direct display time (12 hour system, format HH: mm: SS [AP] m)
%S: seconds since 00:00:00 UTC, January 1, 1970
%S: seconds (00-60)
%T: direct display time (24-hour system)
%X: equivalent to% H:% m:% s
%Z: display time zone
%A: day of the week (Sun SAT)
%A: Sunday Saturday
%B: month (Jan DEC)
%B: January December
%C: direct display of date and time
%D: Day (01-31)
%D: direct display date (mm / DD / yy)
%H: same as% B
%J: the day of the year (001-366)
%M: month (01-12)
%U: the week of the year (00-53) (with Sunday as the first day of the week)
%W: the day of the week (0-6)
%W: the week of the year (00-53) (taking Monday as the first day of the week)
%X: directly display date (mm / DD / yy)
%Y: the last two digits of the year (00.99)
%Y: full year (0000-9999)
If it does not start with a plus sign, it means to set the time, and the time format is mmddhhmm [[CC] YY] [. SS], where mm is the month, DD is the day, HH is the hour, mm is the minute, CC is the first two digits of the year, YY is the last two digits of the year, and SS is the second
-D datestr: displays the time set in datestr (non system time)
–Help: display auxiliary messages
-S datestr: set the system time to the time set in datestr
-U: displays the current Greenwich mean time
–Version: displays the version number
Skip the line after the time is displayed, and then
Show 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′
Note: when you don’t want meaningless 0 (for example, March 7, 1999), you can insert – symbol in the tag. For example, date ‘+% – H:% – M:% – s’ will remove meaningless 0 in hours, minutes and seconds, for example, the original 08:09:04 will become 8:9:4. In addition, only the authorized person (such as root) can set the system time.
After 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 continue to hold the latest correct value when you restart next time.
Example: modify date and time
On the command line, enter:
Date: displays the current time fri Aug 3 14:15:16 CST 2007
Date – s: modify the time in string mode
You can only modify the date without modifying the time. Enter: date – s 2007-08-03
Only modify the time, enter: date – s 14:15:00
When modifying the date and time at the same time, pay attention to double quotation marks. There is a space between the date and time. Enter:
#date -s “2007-08-03 14:15:00”
2. View hardware time
# hwclock
Set hardware time
#Hwlock – Set – date = “07 / 07 / 06 10:19” (month / day / year hour: minute: Second)
3. Synchronization of hardware time and system time
According to the above statement, when the system is restarted, the hardware time will read the system time to realize synchronization. However, when the system is not restarted, the hwlock command needs to be used to realize synchronization.
Synchronization of hardware clock and system clock:
#Hwlock — hctosys (HC represents hardware time and sys represents system time)
Synchronization of system clock and hardware clock: (synchronize the system time to the hardware clock)
# hwclock –systohc
This article comes from the blog “learning, perception and experience”

Recommended Today

OC basis

IOS development interview essential skills chart.png What are objects and what are the objects in OC? An object is an instance of a class; Is an instance created through a class, which is generally called an instance object; Common objects in OC include instance objects, class objects, and metaclass objects; What is a class? What […]