Date: Difference between revisions

From Leo's Notes
This page was last edited on 26 October 2020, at 18:04.
No edit summary
mNo 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.


== Cheat Sheet ==
==Cheat Sheet==
The date utility can be used with a custom format specified by as {{code|+<FORMAT>}}.
The date utility can be used with a custom format specified by as {{code|+<FORMAT>}}.


Here are some commonly used formats.
Here are some commonly used formats.
{| class="wikitable"
{| class="wikitable"
! Format
!Format
! Output
!Output
! Comment
!Comment
|-
|-
| date
|date
| Thu Jun 11 14:00:47 MDT 2020
|Thu Jun 11 14:00:47 MDT 2020
| default output
|default output
|-
|-
| date +%T
|date +%T
| 12:34:56
date +"%H:%M:%S"
| HH:MM:SS
|12:34:56
|HH:MM:SS
Both short and long way
|-
|-
| date +%r
|date +%r
| 09:30:55 PM
|09:30:55 PM
| HH:MM:SS AA
|HH:MM:SS AA
|-
|-
| date +%m-%d-%y
|date +%m-%d-%y
| 12-02-15  
|12-02-15
| MM:DD:YY
|MM:DD:YY
|-
|-
| date +%m-%d-%Y
|date +%m-%d-%Y
| 12-02-2015  
|12-02-2015
| MM:DD:YYYY
|MM:DD:YYYY
|-
|-
| date +%D
|date +%D
| 12/02/2015  
|12/02/2015
| MM/DD/YYYY
|MM/DD/YYYY
|}
|}


Date can also parse a timestamp given with {{code|--date}} argument.
<br />
{{highlight|lang=terminal|code=
 
== Parsing a String Timestamp ==
Date can parse a timestamp by passing it into date with the {{code|--date}} argument and then formatting the output.{{highlight|lang=terminal|code=
$ date --date="Dec 25, 1889" +%m-%d-%y
$ date --date="Dec 25, 1889" +%m-%d-%y
12-25-89
12-25-89
}}


== Unix Timestamp ==
=== Convert Timestamp to Unix Time ===
{{highlight|lang=terminal|code=
## Convert to unix timestamp
## Convert to unix timestamp
$ date --date="Dec 25, 1889" +%s
$ date --date="Dec 25, 1889" +%s
-2525099168
-2525099168


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


 
=== Convert Unix Time to Formatted Timestamp ===
== Setting the Time ==
{{highlight|lang=terminal|code=
You can set the system clock:
## Convert from unix timestamp
$ date -d @1603735163 +"%Y %m %d %H:%M:%S"
2020 10 26 11:59:23
}}
==Setting the System Clock==
Although you really should be using NTP instead, on systems that have no network access, you can set the system clock by running:
{{highlight|lang=terminal|code=
{{highlight|lang=terminal|code=
# date -s "Mar 30 2020 13:16"
# date -s "Mar 30 2020 13:16"
}}
}}
Though, you really should be using NTP instead.




{{Navbox Linux}}[[Category:Linux]]
{{Navbox Linux}}
[[Category:Linux]]


[[Category:LinuxUtilities]]
[[Category:LinuxUtilities]]

Revision as of 18:04, 26 October 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

date +"%H:%M:%S"

12:34:56 HH:MM:SS

Both short and long way

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


Parsing a String Timestamp

Date can parse a timestamp by passing it into date with the --date argument and then formatting the output.

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

Unix Timestamp

Convert Timestamp to Unix Time

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

## Or to nanoseconds by appending 9 zeros
$ date --date="Dec 25, 1998" +%s000000000
914569200000000000

Convert Unix Time to Formatted Timestamp

## Convert from unix timestamp
$ date -d @1603735163 +"%Y %m %d %H:%M:%S"
2020 10 26 11:59:23

Setting the System Clock

Although you really should be using NTP instead, on systems that have no network access, you can set the system clock by running:

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