Linux date command example

date command in Linux with Examples

date command displays the current time and date (of the system) in the given FORMAT. date command is also used by system administrator to set the system date.

Invoking ‘date’command with no FORMAT argument displays system date in dafault FORMAT. The default FORMAT depends on the ‘LC_TIME’ environment variable. In the default C locale, this format is ‘’+%a %b %e %H:%M:%S %Z %Y’’, so the output looks like ‘Mon Aug 3 00:00:00 IST 2020’. Default format includes the abbreviated day name, abbreviated month name, day of the month, the time separated by colons, the time zone name, and the year.

Arguments that are mandatory to long options are mandatory for short options also.

Options

Tag Description
-d, —date=STRING display time described by STRING, not the current time of the system or ‘now’
-f, —file=DATEFILE like —date; once for each line of DATEFILE, a line in DATEFILE may contain text like ‘2 days ago’ or ‘now’ or ‘tomorrow’ or ‘next Monday’, etc.
-I[FMT], —iso-8601[=FMT] output date/time in ISO 8601 format. FMT=’date’ for date only (the default), ‘hours’, ‘minutes’, ‘seconds’, or ‘ns’ for date and time to the indicated precision.
-R, —rfc-2822 output date and time in RFC 2822 format.
—rfc-3339=FMT output date/time in RFC 3339 format. FMT=’date’, ‘seconds’, or ‘ns’ for date and time to the indicated precision.
-s, —set=STRING set time described by STRING
-u, —utc, —universal print or set Coordinated Universal Time (UTC)
—help display help on command line and exit
—version output version information and exit

Arguments

FORMAT argument can consist of several format specifiers, these specifiers are of various kinds, like Time Conversion specifiers, Date Conversion specifiers or literals.

If given an argument that starts with a ‘+’, ‘date’ prints the current date and time in the format defined by that argument. Arguments can consist of format conversion specifiers. Format conversion specifiers are described below.

Time Conversion Specifiers: %[HIklMNpPrRsSTXzZ]

Literal conversion specifiers: %[%nt]

Time Specifier Description
%H hour (’00’. ’23’), 24 hour time format.
%I hour (’01’. ’12’) 12 hour time format
%k hour is space padded (‘ 0’. ‘23’); equivalent to ‘%_H’. This is a GNU extension.
%l hour, space padded (‘ 1’. ‘12’); equivalent to ‘%_I’. This is a GNU extension
%M minute (‘00’. ‘59’)
%N nanoseconds (‘000000000’. ‘999999999’). This is a GNU extension.
%p locale’s equivalent of either ‘AM’ or ‘PM’; blank in many locales. Noon is treated as ‘PM’ and midnight as ‘AM’.
%P like ‘%p’, except lower case. This is a GNU extension.
%r locale’s 12-hour clock time (e.g., ‘12:52:43 PM’)
%R 24-hour hour and minute. Same as ‘%H:%M’ (e.g., 12:52)
%s seconds since the epoch, i.e., since 1970-01-01 00:00:00 UTC. Leap seconds are not counted unless leap second support is available.
%S second (‘00’. ‘60’). This may be ‘60’ if leap seconds are supported.
%T 24-hour hour, minute, and second. Same as ‘%H:%M:%S’, (e.g., 13:00:54)
%X locale’s time representation (e.g., ‘01:03:11 PM’)
%z RFC 2822/ISO 8601 style numeric time zone (e.g., ‘-0600’ or ‘+0530’), or nothing if no time zone is determinable. This value reflects the numeric time zone appropriate for the current time. Time zone is applied using the time zone rules specified by the ‘TZ’ environment variable. The time (and optionally, the time zone rules) can be overridden by the ‘—date’ option.
%:z RFC 3339/ISO 8601 style numeric time zone with ‘:’ (e.g., ‘-06:00’ or ‘+05:30’), or nothing if no time zone is determinable. This is a GNU extension.
%::z Numeric time zone to the nearest second with ‘:’ (e.g., ‘+05:30:00’)
%. z locale’s time representation (e.g., ‘01:03:11 PM’)
%Z alphabetic time zone abbreviation (e.g., ‘EDT’ or ‘IST’), or nothing if no time zone is determinable.
%. z locale’s time representation (e.g., ‘01:03:11 PM’)
%. z locale’s time representation (e.g., ‘01:03:11 PM’)
%. z locale’s time representation (e.g., ‘01:03:11 PM’)
-f, —file=DATEFILE like —date; once for each line of DATEFILE
Читайте также:  Embedded linux systems design and development

Date conversion specifiers: %[aAbBcCdDeFgGhjmuUVwWxyY]

Date Specifier Description
%a locale’s abbreviated weekday name (e.g., ‘Sun’)
%A locale’s full weekday name, variable length (e.g., ‘Sunday’)
%b locale’s abbreviated month name (e.g., ‘Jan’)
%B locale’s full month name, variable length (e.g., ‘January’)
%c locale’s date and time (e.g., ‘Wed 05 Aug 2020 02:39:00 PM IST’)
%C century.This is like ‘%Y’, except the last two digits are omitted. For example, it is ‘20’ if ‘%Y’ is ‘2000’.
%d day of month (e.g., ‘05’)
%D date; same as ‘%m/%d/%y’ (e.g., ‘08/05/20’)
%e day of month, space padded; same as ‘%_d’
%F full date in ISO 8601 format; same as ‘%Y-%m-%d’. This is a good choice for a date format, as it is standard and is easy to sort in the usual case where years are in the range 0000. 9999. (e.g., ‘2020-08-05’)
%g year corresponding to the ISO week number, but without the century (range ‘00’ through ‘99’). This has the same format and value as ‘%y’, except that if the ISO week number (see ‘%V’) belongs to the previous or next year, that year is used instead.
%h same as ‘%b’
%j day of year (‘001’. ‘366’)
%m month (‘01’. ‘12’)
%u day of week (‘1’. ‘7’) with ‘1’ corresponding to Monday
%U week number of year, with Sunday as the first day of the week (‘00’. ‘53’). Days in a new year preceding the first Sunday are in week zero.
%V ISO week number, that is, the week number of year, with Monday as the first day of the week (‘01’. ‘53’). If the week containing January 1 has four or more days in the new year, then it is considered week 1; otherwise, it is week 53 of the previous year, and the next week is week 1.
%w day of week (‘0’. ‘6’) with 0 corresponding to Sunday
%W week number of year, with Monday as first day of week (‘00’. ‘53’). Days in a new year preceding the first Monday are in week zero.
%x locale’s date representation (e.g., ‘08/05/2020’)
%y last two digits of year (‘00’. ‘99’)
%Y year. This is normally at least four characters,
% week number of year, with Monday as first day of week (‘00’. ‘53’). Days in a new year preceding the first Monday are in week zero.
%w

Examples

1. Display date with no options or default format

$ date Wed Aug 5 15:02:59 IST 2020

2. Display universal date and time

$ date -u Wed Aug 5 09:35:18 UTC 2020

3. Display a given string in date format. Note, this does not affect the systems’ actual date and time value.

Читайте также:  Linux copy directory content to another directory

$ date —date=20200810 Mon Aug 10 00:00:00 IST 2020

4. To display current date and time.

$ date --date='now' Wed Apr 14 13:51:45 IST 2021

5. To print the date of the day before yesterday:

$ date --date='2 days ago' Mon Apr 12 13:50:15 IST 2021

6. To print the date next monday.

$ date --date='next monday' Mon Apr 19 00:00:00 IST 2021

7. To display the date 84 days ago.

$ date --date='84 days ago' Wed Jan 20 13:56:33 IST 2021

8. To display the date 2 months ago.

$ date --date='2 months ago' Fri Jun 5 15:55:13 IST 2020

9. To print the date of the day three months and two days hence (from today).

$ date --date='3 months 2 day' Fri Jul 16 14:00:59 IST 2021

10. Use —file option to show date mentioned on each line of the file in the DATEFILE. Let us have a DATEFILE as datefile.txt

$ cat >> datefile.txt next month Sept 23, 2020 today next monday Ctrl+D $ cat datefile.txt next month Sept 23, 2020 today next monday $ date --file=datefile.txt Sat Sep 5 16:08:05 IST 2020 Wed Sep 23 00:00:00 IST 2020 Wed Aug 5 16:08:05 IST 2020 Mon Aug 10 00:00:00 IST 2020

11. To print the day (weekday) of Christmas in the current year:

12. To print the current weekday, full month name and the day of the month:

Источник

Date Command in Linux: How to Set, Change, Format and Display Date

Linux date command displays and sets the system date and time. This command also allows users to print the time in different formats and calculate future and past dates.

Read on to learn how to use the date command in Linux.

How to use Linux date command

  • A system running Linux
  • A user account with root privileges
  • Access to a terminal window/command line

Linux date Command Syntax

The syntax for the date command is:

How to Use date Command in Linux

To show the current system time and date, type in the date command:

Date command in linux

The output displays the day of the week, day of the month, month, year, current time, and time zone. By default, the date command is set to the time zone of the operating system.

The -d option allows users to operate on a specific date. For example, we can type in the following command:

See an older date

You can use the —date command to display the given date string in the format of a date. This command does not affect the system’s actual date and time values, and it only prints the requested date. For example:

Pull a date from a string

Note: Learn how can you create a script using the printf command to display the current date.

Linux date Command Format Options

To format the date command’s output, you can use control characters preceded by a + sign. Format controls begin with the % symbol and are substituted by their current values.

Here, the %Y character is replaced with the current year, %m with month, and %d with the day of the month:

date +"Year: %Y, Month: %m, Day: %d"

Format the current date

Here are another two formatting examples:

Another date format example

date +"Week number: %V Year: %y"

Another formatting example

These are the most common formatting characters for the date command:

    • %D – Display date as mm/dd/yy
    • %Y – Year (e.g., 2020)
    • %m – Month (01-12)
    • %B – Long month name (e.g., November)
    • %b – Short month name (e.g., Nov)
    • %d – Day of month (e.g., 01)
    • %j – Day of year (001-366)
    • %u – Day of week (1-7)
    • %A – Full weekday name (e.g., Friday)
    • %a – Short weekday name (e.g., Fri)
    • %H – Hour (00-23)
    • %I – Hour (01-12)
    • %M – Minute (00-59)
    • %S – Second (00-60)

    To see all formatting options, run date —help or the man command man date in your terminal.

    Set or Change Date in Linux

    To change the system clock manually, use the set command. For example, to set the date and time to 5:30 PM, May 13, 2010, type:

    Manual time set in Linux

    Most Linux distributions have the system clock synchronized using the ntp or the systemd-timesyncd services, so be careful when the setting the clock manually.

    Display Past Dates

    Use the —date option to display past dates in Linux. The date command accepts values such as «tomorrow» , «Friday» , «last Friday» , «next Friday» , «next week» , and similar. So, use the following strings to print past dates::

    Format to

    Yesterday

    Time ten seconds ago format in linux

    Display Future Dates

    The —date option can also display future dates. Like with past dates, you can type in strings to print upcoming dates:

    Date next monday format in linux

    Date four days from now format in linux

    Tomorrow

    Display the Date String at Line of File

    The —file option prints the date string present at each line of the file. Unlike the —date option, —file can present multiple date strings at each line.

    This is the syntax for the —file command:

    Here we use the cat command to add dates to a file and then print them with the date command:

    Display dates at each file line in linux

    Display Last Modified Timestamp of a Date File

    When you use the -r option, the date command prints the last modification time of a file. For example, the following command prints the last time the hosts file was changed:

    The last date the file was modified in linux

    Override a Time Zone

    By default, the date command uses the time zone defined in /etc/localtime . To use a different time zone in the environment, set the TZ variable to the desired time zone.

    For example, to switch to New York time, enter:

    Switch to New York time zone in Linux

    Type in the date command to return the system to its default time zone. To see all available time zones, use the timedatectl list-timezones command.

    The date command can also show the local time for a different time zone. For example, to display the local time for 4:30 PM next Monday on the Australian east coast, type:

    date -d 'TZ="Australia/Sydney" 04:30 next Monday'

    Switch to South Australian time in Linux

    Use date with Other Commands

    You can use the date command to create file names that contain the current time and date. The input below creates a backup MySQL file in the format of the current date:

    mysqldump database_name > database_name-$(date +%Y%m%d).sql

    Another common use of the date command is in shell scripts. Below we assign the output of date to the date_now variable:

    Echo the date now variable

    Use Unix Epoch Time (Epoch Converter)

    You can use the date command as an Epoch converter. Epoch, or Unix timestamps, is the number of seconds that have passed since January 1, 1970, at 00:00:00 UTC.

    To show the number of seconds from the epoch to the current day, use the %s format control:

    Epoch seconds in linux

    To see how many seconds passed from epoch to a specific date, enter:

    Epoch seconds from a specific date in Linux

    You now have a good understanding of how to use the date command in Linux. If you are interested in more date/time configuration options for Linux, read How to Set or Change Timezone/Date/Time on Ubuntu.

    Источник

Оцените статью
Adblock
detector