date (Unix)

date (Unix)
Unix date command

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

Note that the implementation of the date command differs between Unix flavors. Specifically the GNU coreutils based command is much different than other POSIX implementations.

Formatting

To format a date provide a string beginning with + .

Format specifiers (format string starts with +)
Specifier Description Values/Example
Day
%a weekday, abbreviated Tue
%A weekday, full Tuesday
%d day of the month (dd), zero padded 22
%e day of the month (dd) 22
%j day of year, zero padded 001-366
%u day of week starting with Monday (1), i.e. mtwtfss 2
%w day of week starting with Sunday (0), i.e. smtwtfs 2
Week
%U week number Sunday as first day of week 00–53
%W week number Monday as first day of week 01–53
%V week of the year 01–53
Month
%m mm month 11
%h Mon Nov
%b Mon, locale's abbreviated Nov
%B locale's full month, variable length November
Year
%y yy two digit year 00–99
%Y ccyy year 2011
%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 11/22/11
%x locale's date representation (mm/dd/yy) 11/22/2011
%F %Y-%m-%d 2011-11-22
Hours
%l (Lowercase L) hour (12 hour clock) 11
%I (Uppercase I) hour (12 hour clock) zero padded 11
%k hour (24 hour clock) 11
%H hour (24 hour clock) zero padded 11
%p locale's upper case AM or PM (blank in many locales) AM
%P locale's lower case am or pm am
Minutes
%M MM minutes 48
Seconds
%s seconds since 00:00:00 1970-01-01 UTC (Unix epoch) 1321962512
%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) 11:48:32 AM
%R hours, minutes (24 hour clock) hh:mm e.g. 11:48
%T hours, minutes, seconds (24-hour clock) 11:48:32
%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 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: Wed, 16 Dec 2009 15:18:11 +0100

--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

To show the time in a different timezone, the TZ environment variable is read, Timezone types is found in /usr/share/zoneinfo

OLDTZ=$TZ
export TZ=GMT; echo "GMT:               `date +\"%F %R (%Z)\"`"
GMT:               2008-10-31 12:30 (GMT)
export TZ=Europe/Stockholm; echo "Stockholm:    `date +\"%F %R (%Z)\"`"
Stockholm:    2008-10-31 13:30 (CET)
export TZ=Asia/Kuala_Lumpur; echo "Kuala Lumpur:        `date +\"%F %R (%Z)\"`"
Kuala Lumpur:        2008-10-31 20:30 (MYT)
export TZ=US/Central; echo "Dallas:             `date +\"%F %R (%Z)\"`"
Dallas:             2008-10-31 07:30 (CDT)
export TZ=$OLDTZ

Other valid time strings

date +"%Y%m%d" -d sunday # GNU date
20060709

date +"%Y%m%d" -d last-sunday # GNU date
20060702

date +"%Y%m%d" -d last-week # GNU date
date -v -1m +"%Y%m%d" # BSD date
20060627

date +"%Y%m%d" -d last-month # GNU date
date -v -1w +"%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

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

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


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • Date (Unix) — date est une commande UNIX permettant d afficher ou d initialiser date et heure du système. Le vendredi 15 mai 2009 à 17h52 (heure française), date renvoie : ven. mai 15 17:52:00 CEST 2009. Commandes Unix Aide arch · apropos ·… …   Wikipédia en Français

  • Date (Unix) — The Unix date command displays the time and date. The super user can use it to set the system clock. Usage With no options, the date command displays the current date and time, including the abbreviated day name, abbreviated month name, day of… …   Wikipedia

  • date (Unix) — Pour les articles homonymes, voir Date (homonymie). date est une commande UNIX permettant d afficher ou d initialiser date et heure du système. Sur un système Linux, la syntaxe est : affichage: date [option]... [+Format] modification: date [ …   Wikipédia en Français

  • Date — For the use of date on Wikipedia, see Wikipedia:Manual of Style (dates and numbers). Date or dates may refer to: Common Calendar date, a day on a calendar Date (metadata), a representation term or class associated with a data element date (Unix) …   Wikipedia

  • Unix — (marque déposée officiellement comme UNIX, parfois aussi écrit comme Unix avec les petites capitalisations) est le nom d un système d exploitation multitâche et multi utilisateur créé en 1969, conceptuellement ouvert et fondé sur une approche par …   Wikipédia en Français

  • UNIX System V — Unix System V, commonly abbreviated SysV (and usually pronounced, though rarely written as System 5), was one of the versions of the Unix operating system. It was originally developed by AT T and first released in 1983. Four major versions of… …   Wikipedia

  • Unix time — Unix time, or POSIX time, is a system for describing points in time, defined as the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of January 1 1970, not counting leap seconds. It is widely used not only on Unix like… …   Wikipedia

  • date — date  утилита Unix для работы с системными часами. Выводит текущую дату и время в различных форматах и позволяет устанавливать системное время. Содержание 1 Реализации 2 Использование 3 Ключи …   Википедия

  • Unix — (officially trademarked as UNIX, sometimes also written as Unix with small caps) is a computer operating system originally developed in 1969 by a group of AT T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Douglas McIlroy, and… …   Wikipedia

  • Date — утилита Unix для работы с системными часами. Выводит текущую дату и время в различных форматах и позволяет устанавливать системное время. Содержание 1 Использование 2 Ключи 3 См. также …   Википедия

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”