Embedded Commands   «Prev 

Useful date Command Options

The Unix date command displays the date and the time of day. The standard output of the date command looks like this:

$ date
Sat Sep 25 14:38:35 PDT 1999

Purpose of the date -d in unix command

In UNIX-like systems, the date command is used to display or set the system date and time. The -d (or --date) option allows you to specify a different date and/or time string rather than using the current system time. This option is particularly useful for performing various date and time calculations or displaying the date in different formats. The -d option is followed by a date string, which can be an absolute date, a relative date, or a combination of both. The date command then parses this string and outputs the result in the default format or a format specified by the user using the +FORMAT option.
Here are a few examples of using the date -d option:
  1. Display the date for 7 days in the future:
    date -d '+7 days'
    
  2. Display the date for 2 hours ago:
    date -d '-2 hours'
    
  3. Display the date for a specific date string (e.g., "2023-03-17 12:00:00"):
    date -d '2023-03-17 12:00:00'
    
  4. Display the day of the week for a specific date:
    date -d '2023-03-17' '+%A'
    

Please note that the -d option is specific to GNU date (commonly found on Linux systems). The BSD date command (found on macOS and some other UNIX systems) uses a different set of options for similar functionality. To achieve the same results on a BSD system, you would use the -j -f options and provide the input format of the date string.

The date command lets you use options to display pieces of the date and time information in a selected format. If you want to display only the day of the month, you would use this command:

$ date +%e
25

Start the format specification with a plus sign. Follow this with one or more percent-sign-marked letters, which indicate which piece of the date you want. The %e represents the day of the month. %b represents the 3-letter abbreviation for the month:

$ date +%b
Sep

To display the month and the day of the month, use the command below.
$ date +%b-%e
Sep-25

The dash in this command is literal. You can insert characters between the percent-sign-marked pieces of the date and they will be displayed literally in your output: Test out these date command options by logging into your UNIX account and typing them in at the shell prompt.