[Switch] linux date command parameters and usage details -- linux view and modify the system time

date
  command The function of the date command is to display and set the system date and time.
  The general format of this command is: date [options] Display the time format (start with +, followed by the format)
  date Set the time format The meanings of the options in the
  command are:
  -d datestr, --date datestr Display the date described by datestr
  -s datestr, --set datestr set the date described by datestr
  -u, --universal show or set the universal time domain
  % H hours (00..23)
  % I hours (01..12)
  % k hours (0. .23)
  % l hours (1..12)
  % M minutes (00..59)
  % p show AM or PM
  % r time (hh: mm: ss AM or PM), 12 hours
  % s from 1970 1 Month 1st 00:00:00 to the current number of seconds
  % S seconds (00..59)
  % T time (24-hour format) (hh:mm:ss)
  % X format of the display time (%H:%M :%S)
  %Z Time zone date field
  %a Abbreviation of the day of the week (Sun..Sat)
  %A Full name of the day of the week (Sunday..Saturday)
  % b Abbreviation of month (Jan..Dec)
  % Full name of month B (January..December)
  % c Date and time (Mon Nov 8 14:12:46 CST 1999)
  %d Day of the month (01. .31)
  % D date (mm/dd/yy)
  % h and %b options are the same
  % j day of the year (001..366)
  % m month (01..12)
  % w day of the week Day (0 means Sunday)
  % W Week of the year (00..53, Monday is the first day)
  % x Display date format (mm/dd/yy)
  % y Last two digits of the year ( 1999 is 99)
  % Y year (for example: 1970, 1996, etc.)
  It should be noted that only superusers can use the date command to set the time, and ordinary users can only use the date command to display the time.



The following environment variables will affect the execution of the date command. Data Collation www.linuxso.com

LANG Determines the default locale that will be used when neither LC_ALL nor the corresponding environment variables (starting with LC_) specify a locale.
LC_ALL determines the locale used to override any locale category value set by LANG or any environment variable starting with LC_.
LC_CTYPE determines the locale used when byte sequences of text data are interpreted as characters (such as single-to-multibyte characters in arguments).
LC_MESSAGES determines the language in which messages should be written.
LC_TIME determines the content of the date and time string written by date.
NLSPATH determines the location of the message catalog for processing LC_MESSAGES.
TZ Specifies the time zone used for times and dates, 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.

Example 1:

#date //Display the current date #date

-s //Set the current time, which can only be set by root privileges, others can only be viewed.

#date -s 20061010 //Set to 20061010, which will set the specific time to empty 00:00:00

#date -s 12:23:23 //Set the specific time, will not change the date

#date -s “ 12:12:23 2006-10-10″ //This can set the full time

CST: China Standard Time, this explanation may be for 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 mainland China, Hong Kong, Macau, Taiwan, Mongolia, Singapore, Malaysia, the Philippines, and Western Australia is +8, which is UTC+8.

GMT: Greenwich Mean Time (old translation Greenwich Mean Time or Greenwich Mean Time; English: Greenwich Mean Time, GMT) refers to the standard time of the Royal Greenwich Observatory located on the outskirts of London, England, because the prime meridian is defined as passing through warp there.

After setting the system time, you need to synchronize to the hardware clock
# clock --systohc

The hardware clock is synchronized with the system clock:

# hwclock --hctosys
or
# clock

--hctosys In the above command, --hctosys means Hardware Clock to SYStem clock.

Synchronize the system clock with the hardware clock:

# hwclock --systohc
or
# clock --systohc

Example 2:



    Display the time in the specified format.
  $ date ‘+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 you !
  Displays the current time in a predefined format.
  # date
  Fri Nov 26 15:20:18 CST 1999
  Set time to 14:36pm.
  # date -s 14:36:00
  Fri Nov 26 14:15:00 CST 1999
  Set the time to November 28, 1999.
  # date -s 991128
  Sun Nov 28 00:00:00 CST 1999
   Set the date one day ago
     --date "1 days ago" +"%Y-%m-%d"
    Date command parameter tips

Because Linux is vague about the man date -d parameter , so the example is as follows:
# -d, --date=STRING display time described by STRING, not `now'
For Linux
[[email protected] ~ ]# date -d next-day +%Y%m%d
20060328
[ [email protected] ~ ]# date -d last-day +%Y%m%d
20060326
[[email protected] ~ ]# date -d yesterday +%Y%m%d
20060326
[[email protected] ~ ]# date -d tomorrow +%Y%m%d
20060328
[[email protected] ~ ]# date -d last-month +%Y%m
200602
[[email protected] ~ ]# date -d next-month +%Y%m
200604
[[email protected] ~ ]# date -d next-year +%Y
2007
------------------------------- -----------
While FreeBSD is different; for example:
For FreeBSD
[[email protected] ~]# date -v -1d +%Y%m%d
20060326
[[email protected] ~ ]# date -v -1m +%Y%m%d
20060227
[[email protected] ~]# date -v -1y +%Y%m%d
20050327
Example 3:
In the linux environment, it is necessary to obtain a few days ago Just use
date -d "x days ago" +%Y%m%d
x to replace the period with a number, if you need a few days ago, write a positive number directly, if you want a few days later, you can directly write a negative number;
date - d "x weeks ago" +%Y%m%d
x is replaced by a number. If you need a positive number a few weeks ago, you can directly write a negative number if you want the date a few weeks ago;
date -d "x years ago" +%Y%m%d
x is replaced by a number. If you need to write a positive number a few years ago, you can directly write a negative number if you want a date a few years later;
see the following example:
[[email protected] ~]# date +%Y%m%d
20100107
is today's date 20100107
[[email protected] ~]# date -d "2 days ago" +%Y%m%d        
20100105

is the date two days ago
[[email protected] ~]# date - d "4 days ago" +%Y%m%d
20100103
is the date four days ago

[[email protected] ~]# date -d "-1 days ago" +%Y%m%d
20100108
is a date The date in days
[[email protected] ~]# date -d "-2 days ago" +%Y%m%d
20100109
The above is the date in two days
[[email protected] ~]# date -d "1 week ago" +%Y%m%d     
20091231 The
above is the date one week ago
[[email protected] ~]# date -d "1 year ago" +%Y%m%d   
20090107 The
above is the date one year ago

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326990847&siteId=291194637