Linux Date Command

How to Use the Date Command in Linux

In this post, we will show you some examples on how to use the date command in Linux. The date command in Linux  can be used to print or set the system date and time. Using the Date Command in Linux its simple, just follow the examples and the syntax below. Let’s get started.

By default when running the date command in Linux, without any arguments it will display the current system date and time:

date
Sat  2 Dec 12:34:12 CST 2017

Syntax

Usage: date [OPTION]... [+FORMAT]
  or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.

Date examples

The following examples will show you how to use the date command to find the date and time from a period of time in the past or future.

1. Find the date 5 weeks in the future

date -d "5 weeks"
Sun Jan  7 19:53:50 CST 2018

2. Find the date 5 weeks and 4 days in the future

date -d "5 weeks 4 days"
Thu Jan 11 19:55:35 CST 2018

3. Get the next month date

date -d "next month"
Wed Jan  3 19:57:43 CST 2018

4. Get the last sunday date

date -d last-sunday
Sun Nov 26 00:00:00 CST 2017

The date command comes with various formatting option, the following examples will show you how to format the date command output.

5. Display the date in yyyy-mm-dd format

date +"%F"
2017-12-03

6. Display date in mm/dd/yyyy format

date +"%m/%d/%Y"
12/03/2017

7. Display only the time

date +"%T"
20:07:04

8. Display the day of the year

date +"%j"
337

9. Formatting Options

%%A literal percent sign (“%“).
%aThe abbreviated weekday name (e.g., Sun).
%AThe full weekday name (e.g., Sunday).
%bThe abbreviated month name (e.g., Jan).
%BLocale’s full month name (e.g., January).
%cThe date and time (e.g., Thu Mar 3 23:05:25 2005).
%CThe current century; like %Y, except omit last two digits (e.g., 20).
%dDay of month (e.g., 01).
%DDate; same as %m/%d/%y.
%eDay of month, space padded; same as %_d.
%FFull date; same as %Y-%m-%d.
%gLast two digits of year of ISO week number (see %G).
%GYear of ISO week number (see %V); normally useful only with %V.
%hSame as %b.
%HHour (00..23).
%IHour (01..12).
%jDay of year (001..366).
%kHour, space padded ( 0..23); same as %_H.
%lHour, space padded ( 1..12); same as %_I.
%mMonth (01..12).
%MMinute (00..59).
%nA newline.
%NNanoseconds (000000000..999999999).
%pLocale’s equivalent of either AM or PM; blank if not known.
%PLike %p, but lower case.
%rLocale’s 12-hour clock time (e.g., 11:11:04 PM).
%R24-hour hour and minute; same as %H:%M.
%sSeconds since 1970-01-01 00:00:00 UTC.
%SSecond (00..60).
%tA tab.
%TTime; same as %H:%M:%S.
%uDay of week (1..7); 1 is Monday.
%UWeek number of year, with Sunday as first day of week (00..53).
%VISO week number, with Monday as first day of week (01..53).
%wDay of week (0..6); 0 is Sunday.
%WWeek number of year, with Monday as first day of week (00..53).
%xLocale’s date representation (e.g., 12/31/99).
%XLocale’s time representation (e.g., 23:13:48).
%yLast two digits of year (00..99).
%YYear.
%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).
%:::zNumeric time zone with “:” to necessary precision (e.g., -04, +05:30).
%ZAlphabetic time zone abbreviation (e.g., EDT).

 

10. Set the system clock

With the date command in Linux, you can also manually  set the system clock using the --set switch, in the following example we will set the system date to 4:22pm August 30, 2017

date --set="20170830 16:22"

See Also – Find Large Files in Linux

Of course, if you use one of our VPS Hosting services, you can always contact and ask our expert Linux admins (via chat or ticket) about date command in linux and anything related to date examples on Linux. They are available 24×7 and will provide information or assistance immediately.

PS. If you liked this post on How to Use the Date Command in Linux please share it with your friends on the social networks using the buttons below or simply leave a reply. Thanks.

Leave a Comment