date (Unix)

From Wikipedia, the free encyclopedia
Jump to: navigation, search
Unix date command

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 are printed or set in Coordinated Universal Time, as if the time zone were UTC+0. Other Unix and Unix-like systems provide extra options.

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 time zone 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 very different from 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.

Format specifiers (format string starts with +)
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 Sep
%b month name, localised abbreviation Sep
%B locale's full month, variable length September
Year
%y two-digit year 00–99
%Y four-digit year 2014
%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 09/22/14
%x locale's date representation 09/22/2014
%F %Y-%m-%d 2014-09-22
Hours
%l hour (12 hour) 6
%I hour (12 hour), zero-filled 06
%k hour (24 hour) 6
%H hour (24 hour), zero-padded 06
%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 26
Seconds
%s seconds since 00:00:00 1970-01-01 UTC (Unix epoch) 1411367219
%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) 06:26:59 AM
%R hours, minutes (24 hour clock) 06:26
%T hours, minutes, seconds (24-hour clock) 06:26:59
%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 time zone, 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 time zone. 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 time zones 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

GNU date BSD date output
$ date +"%Y%m%d" -d sunday $ date -v +sun +"%Y%m%d" 20060709
$ date +"%Y%m%d" -d last-sunday $ date -v -sun +"%Y%m%d" 20060702
$ date +"%Y%m%d" -d last-week $ date -v -1w +"%Y%m%d" 20060627
$ date +"%Y%m%d" -d last-month $ date -v -1m +"%Y%m%d" 20060604
$ date +"%Y%m%d" -d last-year $ date -v -1y +"%Y%m%d" 20050704
$ date +"%Y%m%d" -d next-week $ date -v 1w +"%Y%m%d" 20060711
$ date +"%Y%m%d" -d next-month $ date -v 1m +"%Y%m%d" 20060804
$ date +"%Y%m%d" -d next-year $ date -v 1y +"%Y%m%d" 20070704
$ date +"%Y%m%d" -d "2 days ago" $ date -v -2d +"%Y%m%d" 20060702
$ date +"%Y%m%d" -d "2 months ago" $ date -v -2m +"%Y%m%d" 20060504
$ date +"%Y%m%d" -d "2 years ago" $ date -v -2y +"%Y%m%d" 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
    • April 26, 2013 18:00:
date --set="20130426 18:00"

Options[edit]

-s, --set=string set time described by string

-n do not 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]

References[edit]

  1. ^ Gite, Vivek. "Getting Yesterdays or Tomorrows Day With Bash Shell Date Command". Nixcraft. Retrieved 24 April 2012. 

External links[edit]