The command Date shows the current date and time. It may also be used for displaying or calculating a date in a format that you specify. The super-user (root) will use this to set the clock for the device.

When we use date command without option,date command will show the current system date and time only,Includes weekday, month, date, time zone and year:

[[email protected] ~]# date
Thu Jun 25 13:31:21 EDT 2020

If you want to check on a specific date,please add -d in command.

[[email protected] ~]# date -d "2020-02-10"
Mon Feb 10 00:00:00 EST 2020

Date has many options for displaying the formatting. Provide date by prefixing the formatting string with a plus sign:

[[email protected] ~]# date +"Week number: %V Year: %y"
Week number: 26 Year: 20

The format string is then output with its value replaced by each formatting token. %V is the formatting option where the current week number is displayed, and %y is the last two digits of the year.

Formatting Options

Please run the command date –help to check all option.

[[email protected] ~]# date --help
FORMAT controls the output. Interpreted sequences are:
%% a literal %
%a locale's abbreviated weekday name (e.g., Sun)
%A locale's full weekday name (e.g., Sunday)
%b locale's abbreviated month name (e.g., Jan)
%B locale's full month name (e.g., January)
%c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%C century; like %Y, except omit last two digits (e.g., 20)
%d day of month (e.g., 01)
%D date; same as %m/%d/%y
%e day of month, space padded; same as %_d
%F full date; same as %Y-%m-%d
%g last two digits of year of ISO week number (see %G)
%G year of ISO week number (see %V); normally useful only with %V
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour, space padded ( 0..23); same as %_H
%l hour, space padded ( 1..12); same as %_I
%m month (01..12)
%M minute (00..59)
%n a newline
%N nanoseconds (000000000..999999999)
%p locale's equivalent of either AM or PM; blank if not known
%P like %p, but lower case
%r locale's 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; same as %H:%M
%s seconds since 1970-01-01 00:00:00 UTC
%S second (00..60)
%t a tab
%T time; same as %H:%M:%S
%u day of week (1..7); 1 is Monday
%U week number of year, with Sunday as first day of week (00..53)
%V ISO week number, with Monday as first day of week (01..53)
%w day of week (0..6); 0 is Sunday
%W week number of year, with Monday as first day of week (00..53)
%x locale's date representation (e.g., 12/31/99)
%X locale's time representation (e.g., 23:13:48)
%y last two digits of year (00..99)
%Y year
%z +hhmm numeric time zone (e.g., -0400)
%:z +hh:mm numeric time zone (e.g., -04:00)
%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
%Z alphabetic time zone abbreviation (e.g., EDT)

Override the Timezone

By default date uses the specified time zone which is defined in /etc/localtime. This action can be overridden with the environment variable TZ. For eg:-

[[email protected] ~]# TZ=GMT date
Thu Jun 25 17:56:54 GMT 2020

Valid timezones are defined in the /usr/share/zoneinfo/ file

The following examples show how the date command can be used to find the date and time at different points in time.

[[email protected] ~]# date -d now
Thu Jun 25 13:59:27 EDT 2020
[[email protected] ~]# date -d now
Thu Jun 25 14:00:36 EDT 2020
[[email protected] ~]# date -d yesterday
Wed Jun 24 14:01:30 EDT 2020
[[email protected] ~]# date -d sunday
Sun Jun 28 00:00:00 EDT 2020

Date command have other valid date and time string:- last-week, next-week, last-month, next-month, last-year, and next-year.

Determine which day of the week a given date was

You may add a shell variable to the date output, and then use it later in your scripts for eg:-

[[email protected] ~]# STARTTIME=date
[[email protected] ~]# echo $STARTTIME
Thu Jun 25 14:07:10 EDT 2020
[[email protected] ~]# sleep 5
[[email protected] ~]# echo $STARTTIME
Thu Jun 25 14:07:10 EDT 2020

You can also use date to create filenames that contain the current day:

[[email protected] ~]# tar cfz /backup-'date +%F'.tar.gz /root/backup

Set the Date Manually from the Linux Terminal

If your system is running ntpd and you have set the time zone correctly, you shouldn’t have to change this setting. However, if you find that you need to set the system clock manually, use the — set option. In this example, the date and time will be set at 10:18 p.m. on Friday, 15 April 2019:

[[email protected] ~]# date --set="20190415 22:18"

That’s all about date command.

Thankyou..