Date: Difference between revisions

From Leo's Notes
This page was last edited on 11 June 2020, at 20:09.
mNo edit summary
No edit summary
Line 1: Line 1:
The {{code|date}} unix utility can set or show the current time.
The {{code|date}} unix utility can set or show the current time.


== Setting the Time ==
== Cheat Sheet ==
You can set the system clock:
The date utility can be used with a custom format specified by as {{code|+<FORMAT>}}.
{{highlight|lang=terminal|code=
# date -s "Mar 30 2020 13:16"
}}


Though, you really should be using NTP instead.
Here are some commonly used formats.
 
== Formatting ==
{{code|date}} can be formatted using the {{code|+}} argument followed by the format string. For example:
{{highlight|lang=terminal|code=
# date +"%T"
11:26:25
}}
 
Here are some common formats.
{| class="wikitable"
{| class="wikitable"
! Format
! Format
! Output
! Output
! Comment
|-
| date
| Thu Jun 11 14:00:47 MDT 2020
| default output
|-
|-
| %T
| date +%T
| 12:34:56 (ie: HH:MM:SS)
| 12:34:56
| HH:MM:SS
|-
|-
| %r
| date +%r
| 09:30:55 PM (ie: HH:MM:SS XX)
| 09:30:55 PM
| HH:MM:SS AA
|-
|-
| %m-%d-%y
| date +%m-%d-%y
| 12-02-15 (ie: MM:DD:YY)
| 12-02-15  
| MM:DD:YY
|-
|-
| %m-%d-%Y
| date +%m-%d-%Y
| 12-02-2015 (ie: MM:DD:YYYY)
| 12-02-2015  
| MM:DD:YYYY
|-
|-
| %D
| date +%D
| 12/02/2015 (ie: MM/DD/YYYY)
| 12/02/2015  
| MM/DD/YYYY
|}
|}
Date can also parse a timestamp given with {{code|--date}} argument.
{{highlight|lang=terminal|code=
$ date --date="Dec 25, 1889" +%m-%d-%y
12-25-89
## Convert to unix timestamp
$ date --date="Dec 25, 1889" +%s
-2525099168
## Or to nanoseconds (9 0's)
$ date --date="Dec 25, 1998" +%s000000000
914569200000000000
}}
== Setting the Time ==
You can set the system clock:
{{highlight|lang=terminal|code=
# date -s "Mar 30 2020 13:16"
}}
Though, you really should be using NTP instead.





Revision as of 20:09, 11 June 2020

The date unix utility can set or show the current time.

Cheat Sheet

The date utility can be used with a custom format specified by as +<FORMAT>.

Here are some commonly used formats.

Format Output Comment
date Thu Jun 11 14:00:47 MDT 2020 default output
date +%T 12:34:56 HH:MM:SS
date +%r 09:30:55 PM HH:MM:SS AA
date +%m-%d-%y 12-02-15 MM:DD:YY
date +%m-%d-%Y 12-02-2015 MM:DD:YYYY
date +%D 12/02/2015 MM/DD/YYYY

Date can also parse a timestamp given with --date argument.

$ date --date="Dec 25, 1889" +%m-%d-%y
12-25-89

## Convert to unix timestamp
$ date --date="Dec 25, 1889" +%s
-2525099168

## Or to nanoseconds (9 0's)
$ date --date="Dec 25, 1998" +%s000000000
914569200000000000


Setting the Time

You can set the system clock:

# date -s "Mar 30 2020 13:16"

Though, you really should be using NTP instead.