Date (Unix command)
The date command outputs current or specified date and time and can also set the current system time.
Option flags
[edit]- +”date format codes” Alters the output using the specified date conversion codes which are prefaced with at % sign. [1] date +”Today is day %j of the year %Y and it’s a %A”
Output: Today is day 078 of the year 2025 and it’s a Wednesday
- -d “datestring” or --date==”datestring” Displays the quoted date. Can be used with the + flag. Date strings consist of calendar date items which express day month and year, and time of day items which express hour:minute:second. Either item group is optional and they may appear in either order.[2] Times and dates can be adjusted using relative item expressions such as + or - time unit as well as keywords such as now, yesterday, tomorrow, previous, next, first and last.[3]
Examples:
date -d "Jan 14, 2026 4:33 PM"
date --date="17:51:02 22/12/2020"
date -d "next Tuesday +2 years -8 hours + 3 days"
- -f filename or --file=filename where the referenced file contains one of more date strings on separate lines which are converted either to the default date format or the format specified by the + flag. Incompatible with the -d flag.
- -r or --reference=filename - outputs the last modification time of the file
- -s or --set=”datestring” sets the current date and time to the value of datestring. Requires an account with admin privilege.[4]
- -u or --UTC displays Greenwich Mean Time
Related Environment Variables
[edit]- LC_TIME - changes the language and default order and format in which the date is presented
export LC_TIME=fr_CA.utf8
The value corresponds assigned to LC_TIME refers to files found in /usr/share/i18/locale. The file name is usually of the form languageCode_countryCode where the codes are the two character ISO codes for List of ISO 639 language codes or List of ISO 3166 country codes6 [5]
It is also possible to change the date presentation format by setting the environment variables LANG or LC_ALL however these will also change other presentation formats such as LC_MESSAGE (command error messages) and LC_MONETARY which many not be desirable.[1]
- TZ - Alters the output of the command to reflect the designated time zone.
export TZ=Africa/Nairobi
The directory /usr/share/zoneinfo contains a large group of timezone database files| files describing both current and historical definitions of local times to facilitate the correct reproduction and translation of time information from one time zone and era to another. The files keep track of the offset from Greenwich Mean Time as well as when seasonal adjustments have been scheduled to take place such as the transition to and from Daylight Saving Time (DST). [6]
References
[edit]- ^ a b Kerrisk, Michael (Feb 2, 2025). "date(1) — Linux manual page". man7.org. Retrieved Mar 20, 2025.
- ^ "Date input formats". gnu.org. Retrieved Mar 20, 2025.
- ^ "Relative items in date strings". gnu.org. gnu.org. Retrieved Mar 20, 2025.
- ^ Glass, Graham; King, Ables (2006). Linux for Programmers and Users. Upper Saddle River, NJ: Pearson Prentice Hall. p. 44. ISBN 978-0131-857483.
- ^ "Locale Environment Variables: Locale Names". www.gnu.org. gnu.org. Retrieved Mar 20, 2025.
- ^ Ward, Brian (April 19, 2021). "7.5.1". How Linux Works, 3rd Edition. No Starch Press. ISBN 978-1718-500402.