date Command

Purpose

Displays or sets the date or time.

Syntax

To Set the Date and Time as Root User

/usr/bin/date [-n] [-u] [Date] [+FieldDescriptor ...] 

To Display the Date and Time

/usr/bin/date [-u] [+FieldDescriptor ...]

To adjust the Time in Seconds as root User

/usr/bin/date [-a [+ | -]sss[.fff]

Description

Attention: Do not change the date when the system is running with more than one user.

The date command writes the current date and time to standard output if called with no flags or with a flag list that begins with a + (plus sign). Otherwise, it sets the current date. Only a root user can change the date and time. The date command prints the usage message on any unrecognized flags or input.

The following formats can be used when you set the date with the Date parameter:

  • mmddHHMM[YYyy]
  • mmddHHMM[yy]

The variables to the Date parameter are defined as follows:

Item Description
mm Specifies the month number.
dd Specifies the number of the day in the month.
HH Specifies the hour in the day by using a 24-hour clock.
MM Specifies the minute number.
YY Specifies the first 2 digits of the year.
Note: If you do not specify the first 2 digits of the year, values in the range 70 - 99 refer to the 20th century, 1970 - 1999 inclusive. Similarly, values in the range 00 - 37 refer to years in the 21st century, 2000 - 2037 inclusive.
yy Specifies the last 2 digits of the year.
Note: The date command accepts a 4-digit year as input. For example, if a 4-digit year is specified, the date command tries to set the year to YYyy and fails for values that are out of range (less than 1970 and greater than 2105). For years in the range 2038 - 2105, specify the year in the yyyy format.

The current year is used as the default value when the year is not specified. The system operates in Coordinated Universal Time (CUT).

If you follow the date command with a + (plus sign) and a field descriptor, you can control the output of the command. You must precede each field descriptor with a % (percent sign). The system replaces the field descriptor with the specified value. Enter a literal % as %% (two percent signs). The date command copies any other characters to the output without change. The date command always ends the string with a new-line character.

Flags

Item Description
-a [+ | -] sss[.fff] Slowly adjusts the time by sss.fff seconds (fff represents fractions of a second). This adjustment can be positive or negative. The system's clock is speeded up or slowed down until it is drifted by the number of seconds specified.
-n Does not set the time globally on all systems in a local area network that have their clocks that are synchronized.
-u Displays or sets the time in Coordinated Universal Time (CUT).

Field Descriptors

Item Description
%a Displays the locale's abbreviated weekday name.
%A Displays the locale's full weekday name.
%b Displays the locale's abbreviated month name.
%B Displays the locale's full month name.
%c Displays the locale's appropriate date and time representation (default).
%C Displays the first 2 digits of the four-digit year as a decimal number (00-99). A year is divided by 100 and truncated to an integer.
%d Displays the day of the month as a decimal number (01-31). In a two-digit field, a 0 is used as leading space fill.
%D Displays the date in the format equivalent to %m/%d/%y.
%e Displays the day of the month as a decimal number (1-31). In a two-digit field, a blank space is used as leading space fill.
%h Displays the locale's abbreviated month name (a synonym for %b).
%H Displays the hour (24-hour clock) as a decimal number (00-23).
%I Displays the hour (12-hour clock) as a decimal number (01-12).
%j Displays the day of year as a decimal number (001-366).
%k Displays the 24-hour-clock hour clock as a right-align, space-filled number (0 - 23).
%m Displays the month of year as a decimal number (01-12).
%M Displays the minutes as a decimal number (00-59).
%n Inserts a new-line character.
%p Displays the locale's equivalent of either AM or PM.
%r Displays 12-hour clock time (01-12) using the AM-PM notation; in the POSIX locale, it is equivalent to %I:%M:%S %p.
%S Displays the seconds as a decimal number (00 - 59).
%s Displays the number of seconds since January 1, 1970, Coordinated Universal Time (CUT).
%t Inserts a <tab> character.
%T Displays the 24-hour clock (00-23) in the format equivalent to HH:MM:SS.
%u Displays the weekday as a decimal number in the range 1-7 (Sunday = 7). Refer to the %w field descriptor.
%U Displays week of the year (Sunday as the first day of the week) as a decimal number [00 - 53]. All days in a new year that precede the first Sunday are considered to be in week 0.
%V Displays the week of the year as a decimal number in the range 01-53 (Monday is used as the first day of the week). If the week that contains January 1 has four or more days in the new year, then it is considered week 01. Otherwise, it is week 53 of the previous year.
%w Displays the weekday as a decimal number in the range 0-6 (Sunday = 0). Refer to the %u field descriptor.
%W Displays the week number of the year as a decimal number (00-53) counting Monday as the first day of the week.
%x Displays the locale's appropriate date representation.
%X Displays the locale's appropriate time representation.
%y Displays the last 2 numbers of the year (00-99).
%Y Displays the four-digit year as a decimal number.
%Z Displays either the time-zone name or time-zone offset as applicable. No characters are displayed, if the time-zone is not determined.
%% Displays a % (percent sign) character.

Modified Field Descriptors

The %E and %O field descriptors can be modified to indicate a different format or specification, as described in LC_TIME Category for the Locale Definition Source File Format in Files Reference. If the corresponding keyword (see the era, era_year, era_d_fmt, and alt_digits keywords) is not specified or not supported for the current locale, the unmodified field descriptor value is used.

Item Description
%Ec Displays the locale's alternative appropriate date and time representation.
%EC Displays the name of the base year (or other time period) in the locale's alternative representation.
%Ex Displays the locale's alternative date representation.
%EX Displays the locale's alternative time representation.
%Ey Displays the offset from the %EC field descriptor (year only) in the locale's alternative representation.
%EY Displays the full alternative year representation.
%Od Displays the day of the month using the locale's alternative numeric symbols.
%Oe Displays the day of the month using the locale's alternative numeric symbols.
%OH Displays the hour (24-hour clock) using the locale's alternative numeric symbols.
%OI Displays the hour (12-hour clock) using the locale's alternative numeric symbols.
Item Description
%Om Displays the month using the locale's alternative numeric symbols.
%OM Displays minutes using the locale's alternative numeric symbols.
%OS Displays seconds using the locale's alternative numeric symbols.
%Ou Displays the weekday as a number in the locale's alternative representation (Monday=1).
%OU Displays the week number of the year using the locale's alternative numeric symbols. Sunday is considered the first day of the week.
%OV Displays the week number of the year using the locale's alternative numeric symbols. Monday is considered the first day of the week.
%Ow Displays the weekday as a number in the locale's alternative representation (Sunday =0).
%OW Displays the week number of the year using the locale's alternative numeric symbols. Monday is considered the first day of the week.
%Oy Displays the year (offset from %C) in alternative representation.

Exit Status

This command returns the following exit values:

Item Description
0 The date was written successfully.
>0 An error occurred.

Security

Attention RBAC users: This command can perform privileged operations. Only privileged users can run privileged operations. For more information about authorizations and privileges, see Privileged Command Database in Security. For a list of privileges and the authorizations that are associated with this command, see the lssecattr command or the getcmdattr subcommand.

Examples

  1. To display current date and time, enter:
    date
  2. To set the date and time, enter:
    date 0217142590
     
    For a system that uses CST as its time zone, this command sets the date and time to Sat Feb 17 14:25:00 CST 1990.
    Note: You must have root authority to change the date and time.
  3. To display the date and time in a specified format, enter:
    date +"%r %a %d %h %y (Julian Date: %j)"
    This command displays the date that is shown in Example 2 as:
    02:25:03 PM Fri 17 Feb 90 (Julian Date: 048)

Environment Variables

The following environment variables affect the execution of the date command.

Item Description
LANG Determines the locale to use when both LC_ALL and the corresponding environment variable (beginning with LC_) do not specify a locale.
LC_ALL Determines the locale to be used to override any values for locale categories that are specified by the setting of LANG or any environment variable beginning with LC_.
LC_CTYPE Determines the locale for the interpretation of sequences of bytes of text data as characters (for example, single versus multibyte character in an argument).
LC_MESSAGES Determines the language in which messages are to be written.
LC_TIME Determines the contents of date and time strings that are written by the date command.
NLSPATH Determines the location of message catalogs for the processing of LC_MESSAGES.
TZ Specifies the time zone in which the time and date are written, unless the -u flag is specified. If the TZ variable is not set and the -u flag is not specified, an unspecified system default time zone is used.