The Linux date command can be used to display or set the date and time of the system. In terms of display, you can set the format to be displayed. The format is set to a plus sign followed by several tags, the available tags are listed as follows:
Time:
- %: Print %
- % N: next row
- % T: Skip
- % H: Hour (00 .. 23)
- % I: Hour (01 .. 12)
- % K: Hour (0 .. 23)
- % L: Hour (1 .. 12)
- % M: minute (00 .. 59)
- % P: displays local AM or PM
- % R: direct display time (in 12-hour format: hh: mm: ss [AP] M)
- % S: the number of seconds since January 1, 1970 00:00:00 UTC till now
- % S: Seconds (00 .. 61)
- % T: direct display time (in 24-hour format)
- % X: equivalent to % H: % M: % S
- % Z: display the time zone
Date:
- % A: The day of the week (Sun .. Sat)
- % A: The 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: Display date directly (mm/dd/yy)
- % H: same as % B
- % J: The day of the year (001 .. 366)
- % M: Month (01 .. 12)
- % U: Week (00 .. 53) of the year (the first day of the week on Sunday)
- % W: The day of the week (0 .. 6)
- % W: Week (00 .. 53) of the year (Monday is the first day of the week)
- % X: Display date directly (mm/dd/yy)
- % Y: last two digits of the year (00.99)
- % Y: full year (0000 .. 9999)
If it does not start with a plus sign, the time is set, 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 second digit of the year, ss is the second digit.
Permission: all users.
If you do not want meaningless 0 values (for example,), you can insert the-symbol in the tag, for example, date '+ %-H: %-M: %-S will remove the meaningless 0 in the hour, minute, and second, as if the original 08:09:04 will change. In addition, you can set the system time only when you have the permission (such as root.
After you change the system time as root, remember to write the system time to CMOS using clock-w, in this way, the system time will continue to hold the latest correct value upon the next reboot.
Syntax
Date [-u] [-d datestr] [-s datestr] [-- utc] [-- universal] [-- date = datestr]
[-- Set = datestr] [-- help] [-- version] [+ FORMAT] [MMDDhhmm [[CC] YY] [. ss]
Parameter description:
- -D datestr: display the time set in datestr (non-system Time)
- -- Help: displays 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.
Instance
Show current time
# Date
March 13, May 12 14:08:12 CST 2010
# Date '+ % c'
Wednesday, May 12, 2010 seconds
# Date '+ % d' // display the complete time
05/12/10
# Date '+ % x' // display the digit date, which is expressed by two digits in the year.
May 12, 2010
# Date '+ % t' // Display date. The year is represented by four digits.
14:09:31
# Date '+ % x' // display the 24-hour format
A.M. 39 seconds
Output in your own format
# Date' + usr_time: $1: % M % P-hey'
Usr_time: $ pm-hey
The current date is displayed after the time is displayed.
Date' + % T % n % d'
Show month and number of days
Date' + % B % d'
Display date and set time (12:34:56)
Date -- date '12: 34: 56'