Jump to content

Date (Unix command)

From Wikipedia, the free encyclopedia

date is a Unix command for calendar/clock computations and working with timestamps, particularly the system time. It exists in most Unix-like operating systems, and its primary purpose is to print or set the current time.

It can be used in a shell script to format dates when generating filenames.[1] Flags such as -d (or -v[2]), when supported, can be used to perform some time-based calculations, and others can be done through math on the Unix time of each date.[3][4] It can also be used to convert between date formats and/or time zones.[5]

In the base POSIX specification, its only function is to output the current system time. It accepts two optional command-line arguments:[6]

  • -u selects the UTC/GMT timezone instead of that specified by the environment variable TZ or the system default.
  • + is followed by a C-style timestamp format string to specify how to arrange the date and time components in the output. In the POSIX or C locale, this defaults to "%a %b %e %H:%M:%S %Z %Y". The %e specifier was added to the relevant standards to allow the user to specify a format string equivalent to the traditional default.

In the X/Open Systems Interfaces (XSI) option that extends POSIX to the Single UNIX Specification,[7][8] a second mode is added, that sets the system time. This is used when an 8-, 10-, or 12-digit number is specified as a command-line argument. This input is parsed as a 2-digit month, then a 2-digit day, then a 2-digit hour (in the 24-hour clock), then a 2-digit minute, optionally followed by a 2- or 4-digit year (defaulting to that of the current time). This mode also supports the -u flag.[6]

The exit status of the program is 0 on success and any positive value on failure; individual implementations may specify further meaning.[6][9][10][11]

GNU

[edit]

The version in GNU coreutils adds the following:[12]

  • To set the number of seconds, . followed by a two-digit number may be appended to the other digits.
  • A variety of command-line options:
    • -s or --set=, followed a description, may be used to explicitly set the system time
    • --utc or --universal may be specified instead of -u to input or output UTC
    • Specifying the timestamp(s) to process instead of the current system time:
      • -d or --date=, followed by a description
        • For example, @ followed by a number to specify Unix time, or some other absolute or relative date/time[13][14][15]
      • -f or --file=, followed by a path to a file containing a description on each line
      • -r or --reference=, followed by a path to a file, to use its modification time
    • Adjusting the output format:
      • --debug requests additional information to assist in debugging
      • Predefined timestamp formats:
        • -I or --iso-8601(=) to request ISO 8601 compliance, optionally followed by the level of precision to use: "date" (default), "hours", "minutes", "seconds", or "ns" (this includes the time zone's UTC offset when the time is included)
        • -R or --rfc-email to request RFC 5322 compliance
        • --rfc-3339=, followed by the desired level of precision ("date", "seconds", or "ns"), to request RFC 3339 compliance, separating the date and time with a space instead of "T"
      • Alternative information:
        • --help requests documentation
        • --resolution queries how much precision the timestamps have
        • --version queries the program's version number and related information

The GNU version also adds additional specifiers that can be used in the timestamp format string; some systems that don't support them instead output them as-is without error.[16]

AIX

[edit]

The version on IBM AIX instead adds the following flags:[10]

  • -a, followed by a positive or negative number of seconds, adjusts the system clock by speeding it up or slowing it down until the resulting clock drift matches the amount of time specified.
  • -n disables updating other systems on the same LAN that have time synchronization with it.

OpenBSD

[edit]

The version on OpenBSD also supports -a (using the adjtime function), as well as the following other flags:[11]

  • -f, followed by a timestamp format string used for parsing input
  • -j, to perform input and output without modifying the system time
  • -r, followed by a Unix time in seconds, specifies the timestamp to output
  • -z, followed by a timezone, specifies which one to use when producing output

Additionally, it is compliant with the base POSIX specification, but only partially with the XSI option: an 8-digit timestamp is parsed the same way, but longer lengths are different, because the 2- or 4-digit year is at the start rather than the end. It also allows omitting month, month+day, or month+day+hour, only requiring the minutes to be specified. The suffix '.SS' for seconds is supported like GNU.

Any changes to the system time are tracked in /var/log/wtmp and /var/log/messages.

FreeBSD

[edit]

The version on FreeBSD supports the following flags:[2]

  • (-d was previously used to set the flag for daylight saving time)
  • -f, like OpenBSD
  • -I, like GNU
  • -j, like OpenBSD
  • -n is obsolete and ignored
  • -R is like GNU, though referencing RFC 2822
  • -r is supported in both the GNU (mtime of file) and OpenBSD (seconds since Unix epoch) senses; the latter allows the number to be specified as decimal, hex, or octal
  • (-t was previously used to set a sign-reversed time zone offset)
  • -u, as per POSIX
  • -v, followed by an optional sign, and then either a number and a unit of time, or the name of a month or weekday, to set or shift the date to be output; this option may be repeated to apply multiple changes sequentially
  • -z, like OpenBSD

References

[edit]
  1. ↑ Nemeth, Evi; Snyder, Garth; Hein, Trent; Whaley, Ben (2011). UNIX and Linux System Administration Handbook (4th ed.). New York: Prentice Hall. pp. 45, 398. ISBN 978-0131-48005-6. Google Books 0SIdBAAAQBAJ.
  2. 1 2 "date(1) - display or set date and time". General Commands Manual. FreeBSD 15.1. The FreeBSD Project. 10 November 2025.
  3. ↑ "Formatting and Displaying Dates with Bash Scripts in Linux". The Shell Scripting Guide: From Basics to Advanced Concepts. Vancouver, BC: Squash Labs Inc. 22 October 2023. Archived from the original on 2025-05-30.
  4. ↑ Halpin, Joe (2003–2005) [Archive-name: unix-faq/shell/sh; Posting-Frequency: monthly; Version: $Id: cus-faq.html,v 1.11 2005/09/01 17:39:36 jhalpin Exp $; Copyright (c) 2003 Joe Halpin]. "comp.unix.shell FAQ - Answers to Frequently Asked Questions : 6. How do I do date arithmetic?". Newsgroup: comp.unix.shell. Archived from the original on 2009-09-06 – via cfajohnson.com.
  5. ↑ "GNU Core Utilities Frequently Asked Questions : 25 The date command is not working right". Archived from the original on 2026-10-23.
  6. 1 2 3 "date — write the date and time". Shell & Utilities. The Open Group Base Specifications Issue 8 / IEEE Std 1003.1-2024.
  7. ↑ "Codes : [XSI] X/Open System Interfaces". The Open Group Base Specifications Issue 8 / IEEE Std 1003.1-2024.
  8. ↑ "3. Definitions : 3.424 XSI". The Open Group Base Specifications Issue 8 / IEEE Std 1003.1-2024.
  9. ↑ "Designing Scripts for Cross-Platform Deployment : date". Shell Scripting Primer. Documentation Archive. Apple Inc. 10 March 2014.
  10. 1 2 "date Command". AIX 7.3.0. IBM Documentation. 15 August 2026.
  11. 1 2 "date(1) - display or set date and time". OpenBSD manual pages. 29 April 2021.
  12. ↑ "date(1) - print or set the system date and time". GNU coreutils 9.11. April 2026. Retrieved 2026-11-25 – via man7.org.
  13. ↑ Bruneau, Guy (12 November 2010). "Scripting with Unix Date". InfoSec Diary Blog Archive. SANS Internet Storm Center.
  14. ↑ Yuan, Kai (2020–2024). "date Command in Linux". Baeldung on Linux. Retrieved 2026-11-26.
  15. ↑ Thuermann, Urs (3 December 2012). "Re: time zone and UTC issue". debian-user@lists.debian.org (Mailing list). ygf624jkmyo.fsf@janus.isnogud.escape.de.
  16. ↑ "11.15 Limitations of Usual Tools : date". 11 Portable Shell Programming. Manual for GNU Autoconf (version 2.69). 24 April 2012.