[Comprehensive explanation of Linux commands] 153. Use the date command to manage system time and date

date

Display or set system time and date

overview

date [OPTION]... [+FORMAT]

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

The main purpose

  • Convert time to selected format, default is current.
  • Set the system time.

parameter

format: Output time format.

formatThe available escape sequences are as follows:

  • %%percent sign
  • %aLocally abbreviated weekday name (for example, Sun)
  • %AThe full local weekday name (for example, Sunday)
  • %bLocally abbreviated month name (for example, Jan)
  • %BFull local month name (for example, January)
  • %cLocal date and time (for example, Thu Mar 3 23:05:25 2005)
  • %CCentury, like %Y, but omits the last two digits (e.g., 20)
  • %dDay of the month (for example, 01)
  • %DDate, equivalent to %m/%d/%y
  • %eThe day of the month, the format is padded with spaces, equivalent to %_d
  • %FComplete date; equivalent to %+4Y-%m-%d
  • %gISO standard counting the last two digits of the year in the week
  • %GISO standard counting weeks of year, usually only useful for %V
  • %hEquivalent to %b
  • %Hhour, range (00…23)
  • %Ihour, range (00…23)
  • %jDay of the year, range (001…366)
  • %kHours, padded with spaces, range (0...23), equivalent to %_H
  • %lHours, padded with spaces, range (1…12), equivalent to %_I
  • %mMonth, range (01…12)
  • %MMinutes, range (00…59)
  • %nnewline character
  • %NNanoseconds, range (000000000…000000000)
  • %pUsed to indicate the local AM or PM, or blank if unknown
  • %PSimilar to %p, but in lowercase
  • %qquarter, range (1…4)
  • %rLocal clock time in 12 hours (for example, 11:11:04 PM)
  • %R24 hours per minute; equivalent to %H:%M
  • %sNumber of seconds since 00:00 UTC January 1, 1970
  • %SSeconds, range (00…60)
  • %thorizontal tab
  • %TTime; equivalent to %H:%M:%S
  • %uDay of the week (1…7), 1 represents Monday
  • %UWeek number of the year, starting on Sunday (00…53)
  • %VISO standard counting of weeks, which uses Monday as the start of the week (01…53)
  • %wDay of the week (0…6), 0 represents Sunday
  • %WWeek number of the year, starting on Monday (00…53)
  • %xLocal date representation (for example, 12/31/99)
  • %XLocal time representation (for example, 23:13:48)
  • %yThe last two digits of the year, range (00…99)
  • %Yyears
  • %z+Numerical time zone format in hhmm format (e.g., -0400)
  • %:z+hh:mm format numeric time zone format (for example, -04:00)
  • %::z+Numerical time zone format in hh:mm:ss format (for example, -04:00:00)
  • %:::zNumerical time zone format, adding ':' compared to the previous format to display the necessary precision (for example, -04, +05:30)
  • %ZTime zone abbreviation (such as EDT)

By default, dates pad numeric fields with zeros; the following optional symbols can follow '%':

  • -(hyphen) Do not populate the corresponding field.
  • _(underscore) Fill the corresponding field with spaces.
  • 0(Number 0) Fill the corresponding field with the number 0.
  • +Fill it with the number 0, and add a '+' sign in front of the future year if it is greater than 4 digits.
  • ^Use uppercase where allowed.
  • #Converts default uppercase to lowercase and default lowercase to uppercase when allowed.

After any flags there is an optional field width, such as decimal; then an optional modifier, where available, use E to use the locale's alternative representation and O to use the locale's alternative representation
. Alternative number symbols.

Options

Long options are equivalent to short options

  • -d, --date=STRINGParse the string and output it in the specified format. The string cannot be 'now'.
  • --debugAnnotate the parsed date and send questionable usage to standard error.
  • -f, --file=DATEFILESimilar to --date; processes one row at a time from DATEFILE.
  • -I[FMT], --iso-8601[=FMT]Output according to ISO 8601 format, FMT can be 'date' (default), 'hours', 'minutes', 'seconds', 'ns'.
    For example: 2006-08-14T02:34:56-06:00
  • -R, --rfc-emailOutput according to RFC 5322 format, for example: Mon, 14 Aug 2006 02:34:56 -0600
  • --rfc-3339=FMTOutput according to RFC 3339 format, FMT can be one of 'date', 'seconds', 'ns',
    for example: 2006-08-14 02:34:56-06:00
  • -r, --reference=FILEDisplays the last modified time of the file.
  • -s, --set=STRINGSet the system time based on a string.
  • -u, --utc, --universalDisplays or sets Coordinated Universal Time (UTC).
  • --helpShow help message and exit.
  • --versionDisplay version information and exit.

return value

The return status is success unless illegal options or illegal parameters are given.

example

  • Formatted output:

    • date +"%Y-%m-%d"
      Output: 2009-12-07
  • Output yesterday's date:

    • date -d "1 day ago" +"%Y-%m-%d"
      Output: 2012-11-19
  • Output after 2 seconds:

    • date -d "2 second" +"%Y-%m-%d %H:%M.%S"
      Output: 2012-11-20 14:21.31
  • The legendary 1234567890 seconds:

    • date -d "1970-01-01 1234567890 seconds" +"%Y-%m-%d %H:%M:%S"
      or
    • date -d@1234567890 +"%F %T"
      Output: 2009-02-13 23:02:30
  • Time format conversion:

    • date -d "2009-12-12" +"%Y/%m/%d %H:%M.%S"
      Output: 2009/12/12 00:00.00
  • apache format conversion:

    • date -d "Dec 5, 2009 12:00:37 AM" +"%Y-%m-%d %H:%M.%S"
      Output: 2009-12-05 00:00.37
  • Time travel after format conversion:

    • date -d "Dec 5, 2009 12:00:37 AM 2 year ago" +"%Y-%m-%d %H:%M.%S"
      Output: 2007-12-05 00:00.37
  • Time addition and subtraction operations:

    • date +%Y%m%d# Display year, month and day
    • date -d "+1 day" +%Y%m%d# Display the date of the previous day
    • date -d "-1 day" +%Y%m%d# Display the date of the next day
    • date -d "-1 month" +%Y%m%d# Display the date of the previous month
    • date -d "+1 month" +%Y%m%d# Display the date of the next month
    • date -d "-1 year" +%Y%m%d# Display the date of the previous year
    • date -d "+1 year" +%Y%m%d# Display the date of the next year
  • set time:

    • date -s# Set the current time. Only root privileges can set it. Others can only view it.
    • date -s 20120523# Set to 20120523, which will set the specific time to 00:00:00
    • date -s 01:01:01# Set the specific time and the date will not be changed.
    • date -s "01:01:01 2012-05-23"# This allows you to set all times
    • date -s "01:01:01 20120523"# This allows you to set all times
    • date -s "2012-05-23 01:01:01"# This allows you to set all times
    • date -s "20120523 01:01:01"# This allows you to set all times
  • Sometimes you need to check how long a set of commands takes:

    start=$(date +%s)
    nmap wangchujiang.com &> /dev/null
    end=$(date +%s)
    difference=$(( end - start ))
    # 显示执行时间
    echo $difference seconds.
    
  • When you consider outputting a string with time, for example (Current time: 2019/05/19):

    • Commonly used methods:
      echo "Current time: $(date +"%Y/%m/%d")"
      
    • Another way:
      suffix='Current time:'
      # 注意如果换成单引号就不能替换变量了。
      date +"${suffix} %Y/%m/%d"
      

Note: This command is a command in the GNU coreutils package. For related help information, please view man -s 1 dateor info coreutils 'date invocation'.

Learn from scratchpython

[Learn python from scratch] 92. Use Python’s requests library to send HTTP requests and process responses
[Learn python from scratch] 91. Use decorators and dictionaries to manage request paths in a simple web application
[Learn python from scratch] 93. Use dictionary management Request path
[Learn python from scratch] 89. Use WSGI to build a simple and efficient Web server
[Learn python from scratch] 88. Detailed explanation of WSGI interface: realize simple and efficient web development
[Learn python from scratch] 87. Manually build an HTTP server in Python Implementation and multi-threaded concurrent processing
[Learn python from scratch] 86. In-depth understanding of the HTTP protocol and its role in browser and server communication
[Learn python from scratch] 85. Application of parallel computing technology in Python process pool
[Learn python from scratch] ] 84. In-depth understanding of threads and processes
[Learn python from scratch] 83. Python multi-process programming and the use of process pools
[Learn python from scratch] 82. Chat program implementation based on multi-threading
[Learn python from scratch] 81. Python more Application of thread communication and queue
[Learn python from scratch] 80. Thread access to global variables and thread safety issues
[Learn python from scratch] 79. Thread access to global variables and thread safety issues
[Learn python from scratch] 78. File download case
[ Learn python from scratch] 77. TCP server programming and precautions
[learn python from scratch] 76. Server and client: key components of network communication
[learn python from scratch] 75. TCP protocol: reliable connection-oriented transmission layer communication protocol
[Learn python from scratch] 74. UDP network program: Detailed explanation of port issues and binding information
[Learn python from scratch] 73. UDP network program - sending data
[Learn python from scratch] 72. In-depth understanding of Socket communication and creation of sockets Method
[Learn python from scratch] 71. Network ports and their functions
[Learn python from scratch] 70. Network communication methods and their applications: from direct communication to routers to connect multiple networks
[Learn python from scratch] 69. Network communication and IP address classification analysis
[Learn python from scratch] 68. Greedy and non-greedy modes in Python regular expressions
[Learn python from scratch] 67. The re module in Python: regular replacement and advanced matching technology
[Learn python from scratch] 66 .In-depth understanding of regular expressions: a powerful tool for pattern matching and text processing
[Learn python from scratch] 65. Detailed explanation of Python regular expression modifiers and their applications
[Learn python from scratch] 64. The re.compile method in Python regular expressions Detailed explanation of usage
[Learn python from scratch] 63. Introduction to the re.Match class and its attributes and methods in regular expressions
[Learn python from scratch] 62. Python regular expressions: a powerful string matching tool
[Learn python from scratch] 61. Detailed explanation and application examples of property attributes in Python
[Learn python from scratch] 60. Exploration generator: a flexible tool for iteration
[Learn python from scratch] 59. Iterator: An efficient tool for optimizing data traversal
[Learn python from scratch] 58. Custom exceptions in Python and methods of raising exceptions
[Learn python from scratch] 57. Use the with keyword in Python to correctly close resources
[Learn python from scratch] 56. The importance and application of exception handling in programming
[Learn python from scratch] 55. Serialization and sum in Python Deserialization, application of JSON and pickle modules
[Learn python from scratch] 54. Writing data in memory
[Learn python from scratch] 53. CSV files and Python’s CSV module
[Learn python from scratch] 52. Reading and writing files - Python file operation guide
[Learn python from scratch] 51. Opening and closing files and their applications in Python
[Learn python from scratch] 49. Object-related built-in functions in Python and their usage
[Learn python from scratch] 48 .Detailed explanation of inheritance and multiple inheritance in Python
[Learn python from scratch] 47. The concept and basic use of inheritance in object-oriented programming
[Learn python from scratch] 46. Analysis of __new__ and __init__ methods and singletons in Python Design Patterns
[Learn python from scratch] 45. Class methods and static methods in Python
[Learn python from scratch] 44. Private properties and methods in object-oriented programming
[Learn python from scratch] 43. Examples in Python object-oriented programming Properties and class attributes
[Learn python from scratch] 42. Built-in properties and methods in Python
[Learn python from scratch] 41. python magic method (2)
[Learn python from scratch] 40. python magic method (1)
[Learn python from scratch] 39. Basic object-oriented syntax and application examples
[Learn python from scratch] 38. How to use and import Python packages
[Learn python from scratch] 37. The use and precautions of Python custom modules
[Learn python from scratch] Learn python] 36. Methods and techniques of using pip for third-party package management in Python
[Learn python from scratch] 35. Common Python system modules and their usage
[Learn python from scratch] 34. Detailed explanation of the import and use of Python modules
[ Learn python from scratch] 33. The role of decorators (2)
[Learn python from scratch] 32. The role of decorators (1)
[Learn python from scratch] 31. In-depth understanding of higher-order functions and closures in Python
[From Learn python from scratch】30. In-depth understanding of recursive functions and anonymous functions
【learn python from scratch】29. "Detailed explanation of function parameters" - understand the different uses of Python function parameters
【learn python from scratch】28. Local variables and global variables in Python Variables
[Learn python from scratch] 27. The use of Python functions and nested calls
[Learn python from scratch] 25. Functions: a tool to improve the efficiency of code writing
[Learn python from scratch] 24. String operations and traversal methods in Python
[Learn python from scratch] 23. How to use sets (set) and common operations in Python
[Learn python from scratch] 22. Add, delete, modify, and query dictionary variables in Python
[Learn python from scratch] 21. Python tuples and dictionaries
[Learn python from scratch] 20. Python list operation skills and examples
[Learn python from scratch] 19. Applications of looping through lists and list nesting
[Learn python from scratch] 18. Detailed explanation of the basic operations of Python lists (1)
[From Learning python from scratch】 17. The format method of Python strings (2)
【Learning python from scratch】 16. The format method of Python strings (1)
【Learning python from scratch】 15. In-depth understanding of strings and character set encoding
【From Learning python from scratch】14. Common operations on Python strings (2)
【Learning python from scratch】13. Common operations on Python strings (1)
【Learning python from scratch】12. Python string operations and applications
【Learning python from scratch】 11. Python loop statements and control flow
[Learn python from scratch] 10. Detailed explanation of Python conditional statements and if nesting
[Learn python from scratch] 09. Conditional judgment statements in Python
[Learn python from scratch] 08. Python understands bit operations operator, operator priority
[Learn python from scratch] 07. Detailed explanation of Python operators: assignment, comparison and logical operators
[Learn python from scratch] 06. Use arithmetic operators in Python for calculations and string concatenation
[Learn from scratch] python ] 05. Output and input in Python
[Learn python from scratch] 04. Basics of Python programming: variables, data types and identifiers
[Learn python from scratch] 03. Python interactive programming and detailed explanation of comments
[Learn python from scratch] 02. Introduction to development tools
[Learn python from scratch] 01. Install and configure python

Guess you like

Origin blog.csdn.net/qq_33681891/article/details/132965322