This MedLibrary.org supplementary page on Date (Unix) is provided directly from the open source Wikipedia as a service to our readers. Please see the note below on authorship of this content, as well as the Wikipedia usage guidelines. To search for other content from our encyclopedia supplement, please use the form below:
Related Sponsors
The Unix date command displays the time and date. The super-user can use it to set the system clock.
Contents |
Usage
With no options, the date command displays the current date and time, including the abbreviated day name, abbreviated month name, day of the month, the time separated by colons, the timezone name, and the year. For example:
$date Fri Jul 27 14:12:06 EDT 2007
Formatting
To format a date provide a string beginning with + .
| Specifier | Description | Values/Example |
|---|---|---|
| Day | ||
| %a | weekday, abbreviated | Fri |
| %A | weekday, full | Friday |
| %d | day of the month (dd), zero padded | 10 |
| %e | day of the month (dd) | 10 |
| %j | day of year, zero padded | 000-366 |
| %u | day of week starting with Monday (1), i.e. mtwtfss | 5 |
| %w | day of week starting with Sunday (0), i.e. smtwtfs | 5 |
| Week | ||
| %U | week number Sunday as first day of week | 0–53 |
| %W | week number Monday as first day of week | 0–53 |
| %V | week of the year | 0–53 |
| Month | ||
| %m | mm month | 10 |
| %h | Mon | Oct |
| %b | Mon, locale's abbreviated | Oct |
| %B | locale's full month, variable length | October |
| Year | ||
| %y | yy two digit year | 00–99 |
| %Y | ccyy year | 2008 |
| %g | 2-digit year corresponding to the %V week number | |
| %G | 4-digit year corresponding to the %V week number | |
| Century | ||
| %C | cc century | 00–99 |
| Date | ||
| %D | mm/dd/yy | 10/10/2008 |
| %x | locale's date representation (mm/dd/yy) | 10/10/2008 |
| %F | %Y-%m-%d | |
| Hours | ||
| %l | hour (12 hour clock) | 8 |
| %I | hour (12 hour clock) zero padded | 08 |
| %k | hour (24 hour clock) | 20 |
| %H | hour (24 hour clock) zero padded | 20 |
| %p | locale's upper case AM or PM (blank in many locales) | PM |
| %P | locale's lower case am or pm (really!) | pm |
| Minutes | ||
| %M | MM minutes | 21 |
| Seconds | ||
| %s | seconds since 00:00:00 1970-01-01 UTC (Unix epoch) | 1223670109 |
| %S | SS second | 00–60 (The 60 is necessary to accommodate a leap second) |
| %N | nanoseconds | 000000000–999999999 |
| Time | ||
| %r | hours, minutes, seconds (12-hour clock) | 08:21:49 PM |
| %R | hours, minutes (24 hour clock) | hh:mm e.g. 20:21 |
| %T | hours, minutes, seconds (24-hour clock) | 20:21:49 |
| %X | locale's time representation (%H:%M:%S) | |
| Date and Time | ||
| %c | locale's date and time | Sat Nov 04 12:02:33 EST 1989 |
| Time zone | ||
| %z | -zzzz RFC-822 style numeric timezone | -0500 |
| %Z | time zone (e.g., EDT) nothing if no time zone is determinable | EST |
literals: %n newline %% percent %t horizontal tab
By default, date pads numeric fields with zeroes.
GNU date, but not BSD date, recognizes - (hyphen) do not pad the field and _ (underscore) pad the field with spaces between % and a numeric directive.
TZ Specifies the timezone, unless overridden by command line parameters. If neither is specified, the setting from /etc/localtime is used.
Options
-d, -de=string display time described by string, not now.
-e=datefile like de once for each line of datefile
-s, --set=string set time described by string
-n don't synchronize the clocks on groups of machines using the utility timed(8). By default, if timed is running, date will set the time on all of the machines in the local group. -n inhibites that.
-u Display or set the date in UTC (universal) time.
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY].ss The only valid option for the this form specifies Coordinated Universal Time.
-u GMT ex: Sat Feb 5 14:49:42 GMT 2005
--utc, --universal Coordinated Universal Time local TZ Sat Feb 5 09:49:59 EST 2005
-ITIMESPEC, --iso-8601[=TIMESPEC] output date/time in ISO 8601 format. TIMESPEC=date for date only, hours, minutes, or seconds for date and time to the indicated precision.
--iso-8601 without TIMESPEC defaults to `date'.
-R, --rfc-822 output RFC-822 compliant date string example: Sat Feb 5 09:50:23 EST 2005
--help
The Single Unix Specification (SUS) mandates only one option: -u, where the date and time is printed as if the timezone was UTC+0. Other Unix and Unix-like systems provide extra options.
Examples
date "+%-m/%-d/%y" 7/4/06
date "+%Y%m%d" 20060704
To assign the time to a variable
START=`date '+%r'` echo $START 03:06:02 PM sleep 5 echo $START 03:06:02 PM
N.B. the variable has the time when it was assigned.
Yesterday assigned to variable
DATE=$(date -d yesterday +"%Y%m%d") echo $DATE 20060704
Other valid time strings
date +"%Y%m%d" -d sunday 20060709 date +"%Y%m%d" -d last-sunday 20060702 date +"%Y%m%d" -d last-week 20060627 date +"%Y%m%d" -d last-month 20060604 date +"%Y%m%d" -d last-year 20050704 date +"%Y%m%d" -d next-week 20060711 date +"%Y%m%d" -d next-month 20060804 date +"%Y%m%d" -d next-year 20070704
Setting the date
The XSI extension to the SUS specifies that the date command can also be used to set the date. The new date is specified as an option to date in the format MMddhhmm[[cc]yy], where MM specifies the two-digit numeric month, dd specifies the two-digit numeric day, hh specifies the two-digit numeric hour, mm specifies the two-digit numeric minutes. Optionally cc specifies the first two digits of the year, and yy specifies the last two digits of the year.
Other Unix and Unix-like systems may set different options or date formats for date, for example, on some systems to set the current date and time to September 8, 2004 01:22 you type:
date --set="20040908 01:22"
See also
- List of Unix programs
- Unix time, i.e. number of seconds elapsed since midnight UTC of January 1, 1970 Epoch
- time and date
- Cron process for scheduling jobs to run on a given date.
External links
|
||||||||||||||||||||||||||
Wikipedia content modification information:
- This page was last modified on 23 September 2008, at 21:26.
Wikipedia Authorship and Review
Wikipedia content provided here is not reviewed directly by MedLibrary.org. Wikipedia content is authored by an open community of volunteers and is not produced by or in any way affiliated with MedLibrary.org.
Wikipedia Usage Guidelines
This article is licensed under the GNU Free Documentation License. It uses material from the Wikipedia article on "Date (Unix)".
The URL for this specific entry is:
All Wikipedia text is available under the terms of the GNU Free Documentation License. (See Copyrights for details). Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc.
