Linux Command Reference ยท System Information
Linux date command
Reference for the Linux date command: print and format the current time, convert epoch seconds, do date arithmetic with -d, and use UTC and time zones.
date prints the current date and time, and with the right options it converts between formats, does date arithmetic and shows a moment in any time zone. In scripts it is the standard way to stamp file names, log lines and backup folders.
The GNU version (the one on almost every Linux distribution) understands very flexible input through -d: phrases like next friday, 2 weeks ago or a plain epoch number all work. Output is shaped by a format string that starts with +.
date [OPTION]... [+FORMAT]
date -d STRING [+FORMAT]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
date [-u] [-d STRING | -f FILE | -r FILE] [+FORMAT] date -s STRING
| Option | Description |
|---|---|
-d STRING, --date=STRING |
Show the time described by STRING instead of now. Understands absolute dates, relative phrases and @SECONDS. |
-u, --utc |
Work in UTC instead of the local zone. |
-r FILE, --reference=FILE |
Show the last modification time of FILE. |
-I[FMT], --iso-8601[=FMT] |
ISO 8601 output; FMT is date (default), hours, minutes, seconds or ns. |
-R |
RFC 5322 style, as used in email headers. |
-f FILE, --file=FILE |
Read one date string per line from FILE and print each. |
-s STRING, --set=STRING |
Set the system clock. Needs root, so it is not available in sandboxes. |
Frequent format sequences:
| Sequence | Meaning |
|---|---|
%Y %m %d |
Year, month (01-12), day of month (01-31) |
%F |
Same as %Y-%m-%d |
%H %M %S |
Hour (00-23), minute, second |
%T / %R |
%H:%M:%S / %H:%M |
%s |
Seconds since 1970-01-01 00:00:00 UTC |
%a %A / %b %B |
Short and full weekday / month name |
%j |
Day of the year (001-366) |
%u / %w |
Weekday number, Monday=1 / Sunday=0 |
%V |
ISO week number |
%z %Z |
Numeric offset such as +0100 / zone abbreviation |
%N |
Nanoseconds |
%e |
Day padded with a space |
A flag between % and the letter changes padding: %-d removes it, %_d pads with spaces, %0e pads with zeros.
Exit status: 0 on success; 1 if the date string cannot be parsed (date: invalid date '...').
Usage notes
- Quote the format. Single-quote it (
'+%F %T') so the shell does not split it at spaces. - Month arithmetic overflows.
2024-01-31 + 1 monthis "February 31", which GNU date resolves to March 2. Compute with days when you need predictable results. - Local time versus UTC. Without
-uthe result depends onTZand/etc/localtime. Use-u(orTZ=UTC) for logs and anything compared across machines. - Epoch for math. Convert to
%s, subtract, divide: this avoids parsing calendars by hand. Remember@Nis the reverse conversion. - Safe file names. Use
$(date +%F_%H-%M-%S); colons in names break on some file systems and tools. - In Bash 5 you can skip a process:
printf '%(%F)T\n' -1formats the current time without startingdate. See printf and date. - GNU only. BSD and macOS
dateuse-vfor adjustments and lack-d. Scripts for those systems need another approach (or GNU coreutils). - Setting the clock is normally the job of a time service; see
timedatectl.
Related
cal,timedatectl,stat,locale- Lessons: printf and date, First script