Date (Unix)

From Wikipedia, the free encyclopedia

The correct title of this article is date. The initial letter is shown capitalized due to technical restrictions.

date is a Unix command that displays the current time and date of the system.

Contents

[edit] Usage

The date command, invoked with no options, prints out the date and time according to the scheme where the abbreviated day name, abbreviated month name, day of the month, time separated by colons, timezone name, and year is printed, for example.

  Tue Sep  11 13:37:00 EDT 2001

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 may provide extra options.

[edit] Formatting options

The date command provides the ability to specify a formatting string to change the output.

[edit] Specifiers

%m mm month (01..12)

%h Mon %b locale's abbreviated month (Jan..Dec)

%B locale's full month, variable length ex:(January..December)

%d dd (01..31) %e ( 1..31)

%y yy two digit year (00..99)

%Y ccyy year (1970...) %C cc century [00-99]

%g 2-digit year corresponding to the %V week number %G 4-digit

%D mm/dd/yy %x locale's date representation (mm/dd/yy) %F %Y-%m-%d

%I HH hours (01..12) %l 1..12 %H 00..23 %k 0..23

%M MM minutes (00..59)

%p locale's upper case AM or PM (blank in many locales) %P locale's lower case am or pm (really!)

%S SS second (00..60)) The 60 is necessary to accommodate a leap second %N nanoseconds (000000000..999999999)

%r time, 12-hour (hh:mm:ss x.M) %R 24-hour (hh:mm) %T 24-hour (hh:mm:ss)

%X locale's time representation (%H:%M:%S)

%c locale's date and time ex: Sat Nov 04 12:02:33 EST 1989

%j day of year (001..366)

%u day of week (1..7); 7 represents Sunday mtwtfss %w (0..6); 0 represents Sunday smtwtfs

%a locale's abbreviated weekday (Sun..Sat)

%A locale's full weekday, variable length (Sunday..Saturday)

%U week number Sunday as first day of week (00..53)

%W week number Monday as first day of week (00..53) %V (01..53)

%z -zzzz RFC-822 style numeric timezone (-0500)

%Z time zone (e.g., EDT) nothing if no time zone is determinable

%s seconds since 00:00:00 1970-01-01 UTC

literals: %n newline ; %% percent; %t horizontal tab

By default, date pads numeric fields with zeroes.

GNU (not BSD) date recognizes the following modifiers between `%' and a numeric directive.

- (hyphen) do not pad 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.

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

date and time seconds from the Epoch. ( i.e. Dec 31 19:00:00 EST 1969 ) -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

[edit] Examples

date "+%_H == %k %-m/%-d/%y" 
1/2/50
date "+%Y%m%d"
20061225

In UNIX, you can assign the date value to a variable.

MY_DATE=`date '+%m/%d/%Y %T%p'`
echo $MY_DATE
02/08/2007 15:03:43PM

[edit] 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"

[edit] See also

[edit] External links