date (Unix)
The Unix date command displays and sets the time and date of the system clock. Setting the clock is restricted to the superuser.
The Single Unix Specification (SUS) mandates only one option: -u, where the date and time is printed or set in Coordinated Universal Time, as if the timezone were UTC+0. Other Unix and Unix-like systems provide extra options.
Contents |
Displaying the date [edit]
Usage [edit]
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
Note that the implementation of the date command differs between Unix flavors (see references below). Specifically the GNU coreutils based command is much different than other POSIX implementations.
Formatting [edit]
To format a date, a parameter string beginning with a plus sign (+) is given. The formatting specifiers below are supported by most implementations.
| Specifier | Description | Values or example |
|---|---|---|
| Day | ||
| %a | weekday, abbreviated | Mon |
| %A | weekday, full | Monday |
| %d | day of the month, two digits, zero filled | 08 |
| %e | day of the month | 8 |
| %j | day of year, zero filled | 001–366 |
| %u | day of week from Monday to Sunday | 1–7 |
| %w | day of week from Sunday to Saturday | 0–6 |
| Week | ||
| %U | week number, Sunday as first day of week | 00–53 |
| %W | week number, Monday as first day of week | 00–53 |
| %V | ISO standard week of the year | 01–53 |
| Month | ||
| %m | two-digit month number | 01–12 |
| %h | month name, abbreviated | May |
| %b | month name, localised abbreviation | May |
| %B | locale's full month, variable length | May |
| Year | ||
| %y | two-digit year | 00–99 |
| %Y | four-digit year | 2013 |
| %g | two-digit year corresponding to the %V week number | |
| %G | four-digit year corresponding to the %V week number | |
| Century | ||
| %C | two century digits from year | 00–99 |
| Date | ||
| %D | mm/dd/yy | 05/20/13 |
| %x | locale's date representation | 05/20/2013 |
| %F | %Y-%m-%d | 2013-05-20 |
| Hours | ||
| %l | hour (12 hour) | 9 |
| %I | hour (12 hour), zero-filled | 09 |
| %k | hour (24 hour) | 9 |
| %H | hour (24 hour), zero-padded | 09 |
| %p | locale's upper case AM or PM (blank in many locales) | AM |
| %P | locale's lower case am or pm | am |
| Minutes | ||
| %M | two-digit minute number | 25 |
| Seconds | ||
| %s | seconds since 00:00:00 1970-01-01 UTC (Unix epoch) | 1369041901 |
| %S | two-digit second number | 00–60 (Includes 60 to accommodate a leap second) |
| %N | nanoseconds | 000000000–999999999 |
| Time | ||
| %r | hours, minutes, seconds (12-hour clock) | 09:25:01 AM |
| %R | hours, minutes (24 hour clock) | 09:25 |
| %T | hours, minutes, seconds (24-hour clock) | 09:25:01 |
| %X | locale's time representation | 11:07:26 AM |
| Date and time | ||
| %c | locale's date and time | Sat Nov 04 12:02:33 EST 1989 |
| Time zone | ||
| %z | RFC-822 style numeric time zone | -0500 |
| %Z | time zone name; nothing if no time zone is determinable | EST, EDT |
literals: %n newline %% percent %t horizontal tab
By default, date normally fills numeric fields with zeroes. GNU date, but not BSD date, recognizes a modifier between the per cent sign (%) and the format specifier:
- hyphen (-): do not fill the field
- underscore (_): pad the field with spaces
TZ Specifies the timezone, unless overridden by command line parameters. If neither is specified, the setting from /etc/localtime is used.
GNU date options [edit]
-d, --date=string display time described by string, not now. It is a human readable format such as "next Thursday" or "1 month ago". A date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers. This is also known as relative GNU date formats.[1] Here are a few examples of relative date:
date --date="1 days ago" date --date="yesterday" date --date='10 month ago' date --date='2 hour ago' date --date='Second Friday'
-e=datefile like de once for each line of datefile
-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: Wed, 16 Dec 2009 15:18:11 +0100
Examples [edit]
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
The TZ environment variable specifies the timezone. Valid values are in /usr/share/zoneinfo
TZ=GMT; echo "GMT: `date +\"%R (%Z)\"`" GMT: 12:30 (GMT) TZ=Europe/Stockholm; echo "Stockholm: `date +\"%R (%Z)\"`" Stockholm: 13:30 (CET) TZ=Asia/Kuala_Lumpur; echo "Kuala Lumpur: `date +\"%R (%Z)\"`" Kuala Lumpur: 20:30 (MYT) TZ=US/Central; echo "Dallas: `date +\"%R (%Z)\"`" Dallas: 07:30 (CDT)
Converting between timezones Example: What time is it in Moscow when it will be 17:35 in Los Angeles
$ TZ=Europe/Moscow date "+%F %R (%Z%z)" -d 'TZ="America/Los_Angeles" 17:35' 2013-03-22 04:35 (MSK+0400)
Other valid time strings
date +"%Y%m%d" -d sunday # GNU date date -v +sun +"%Y%m%d" # BSD date 20060709 date +"%Y%m%d" -d last-sunday # GNU date date -v -sun +"%Y%m%d" # BSD date 20060702 date +"%Y%m%d" -d last-week # GNU date date -v -1w +"%Y%m%d" # BSD date 20060627 date +"%Y%m%d" -d last-month # GNU date date -v -1m +"%Y%m%d" # BSD date 20060604 date +"%Y%m%d" -d last-year # GNU date date -v -1y +"%Y%m%d" # BSD date 20050704 date +"%Y%m%d" -d next-week # GNU date date -v 1w +"%Y%m%d" # BSD date 20060711 date +"%Y%m%d" -d next-month # GNU date date -v 1m +"%Y%m%d" # BSD date 20060804 date +"%Y%m%d" -d next-year # GNU date date -v 1y +"%Y%m%d" # BSD date 20070704
date +"%Y%m%d" -d "2 days ago" # GNU date date -v -2d +"%Y%m%d" # BSD date 20060702
date +"%Y%m%d" -d "2 months ago" # GNU date date -v -2m +"%Y%m%d" # BSD date 20060504
date +"%Y%m%d" -d "2 years ago" # GNU date date -v -2y +"%Y%m%d" # BSD date 20040704
To show the time in seconds since 1970-01-01 (Unix epoch):
date +"%s" -d "Fri Apr 24 13:14:39 CDT 2009" 1240596879
To convert Unix epoch time (seconds since 1970-01-01) to a human readable format:
date -d "UTC 1970-01-01 1240596879 secs" Fri Apr 24 13:14:39 CDT 2009
Or:
date -ud @1000000000 Sun Sep 9 01:46:40 UTC 2001
Output based on locale [edit]
> export LC_ALL="" > date Mon Apr 23 20:47:18 EDT 2012 > export LC_ALL="en_US.UTF-8" > date Mon Apr 23 20:46:59 EDT 2012 > export LC_ALL="de_DE" > date Mo 23. Apr 20:47:05 EDT 2012 > export LC_ALL="danish" > date man apr 23 20:48:09 EDT 2012
Setting the date [edit]
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.
The date command from GNU Coreutils (often used on Linux) allows another method with a different format,
- for example to set the date and time to
- March 26, 2013 18:00:
date --set="20130426 18:00"
Options [edit]
-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 inhibits that.
See also [edit]
- List of Unix programs
- Unix time, i.e. number of seconds elapsed since midnight UTC of January 1, 1970 Epoch
- DATE DOS command
- TIME DOS command
- Cron process for scheduling jobs to run on a given date.
References [edit]
- ^ Gite, Vivek. "Getting Yesterdays or Tomorrows Day With Bash Shell Date Command". Nixcraft. Retrieved 24 April 2012.
External links [edit]
- : write the date and time – Commands & Utilities Reference, The Single UNIX® Specification, Issue 7 from The Open Group
- : print or set the system date and time – Linux User Commands Manual
- – FreeBSD General Commands Manual
- UNIX date command examples.
|
||||||||||||||||||||||||||||||||