Date (Unix command): Difference between revisions

Content deleted Content added
GelvinM (talk | contribs)
Added note on related environment variables that can change the output of the command. Fixed ISBN of Linux for Programmers book.
m modify subheading redirect
Tags: Redirect target changed Visual edit
 
(25 intermediate revisions by 8 users not shown)
Line 1:
#REDIRECT [[System time#Retrieving system time]]
The '''date''' command outputs current or specified date and time and can also set the current system time.
 
{{Rcat shell|
==Option flags==
{{R to related topic}}
 
}}
*+”''date format codes”'' Alters the output using the specified date conversion codes which are prefaced with at % sign. <ref name=KerriskLocale>{{cite web |last=Kerrisk |first=Michael |date=Feb 2, 2025 |title=date(1) — Linux manual page |url=https://man7.org/linux/man-pages/man1/date.1.html |access-date=Mar 20, 2025 |website=man7.org |publisher=}}</ref> 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.<ref>{{cite web |author=<!-- not stated --> |title=Date input formats |url= https://www.gnu.org/software/coreutils/manual/html_node/Date-input-formats.htmlwebsite=gnu.org |publisher=gnu.org |access-date=Mar 20, 2025}} </ref> 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.<ref>{{cite web |author=<!-- not stated --> |title=Relative items in date strings |url= https://www.gnu.org/software/coreutils/manual/html_node/Relative-items-in-date-strings.html|website=gnu.org |publisher=gnu.org |access-date=Mar 20, 2025}}</ref>
 
''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.<ref>{{cite book|last1=Glass |first1=Graham | last2=King| first2=Ables|date=2006 |title=Linux for Programmers and Users |___location=Upper Saddle River, NJ |publisher=Pearson Prentice Hall|page=44|isbn=978-0131-857483}} </ref>
 
*-u or --UTC displays [[Universal time|Greenwich Mean Time]]
 
==Related Environment Variables==
 
*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 [[language|List of ISO 639 language codes]] or [[country|List of ISO 3166 country codes6]] <ref>{{cite web| author=<!-- not stated --> |title=Locale Environment Variables: Locale Names |url=https://www.gnu.org/software/gettext/manual/html_node/Locale-Names.html|website=www.gnu.org |publisher=gnu.org |access-date=Mar 20, 2025}}</ref>
 
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.<ref name=KerriskLocale></ref>
 
 
 
*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 [[tz database| 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|Daylight Saving Time]] (DST). <ref>{{cite book |last=Ward |first=Brian |date=April 19, 2021 |title=How Linux Works, 3rd Edition |publisher=No Starch Press |section=7.5.1 |isbn=978-1718-500402}} </ref>
 
==References==
{{reflist}}
[[Category:Standard Unix programs]]