Using The Date Command With Examples

In this easy guide I will be showing you how to use the Linux date command. The date command can be used to see what the date is, what time it is, as-well as set system time (with root/sudo privileges). I will give a few examples as-well as explain the options below since you can format the output to get exactly the information you are looking for as-well as formatted the way you want it.

Formatting The Date Command Output

When using the date command you can either simply run “date” by itself or you can format the output using something similar to the following command.

date +"%B %d %Y"
October 16 2017

Notice the % signs followed by the characters? Those will be replaced by their current values based on the table below. You also don’t have to use a blank space as your seperator, you can use pretty much anything such as a dash (-) or slant (/). If the specific value you are looking for is not listed, you can see a full list of options by running date –help.

%alocale’s abbreviated weekday name (e.g., Sun)
%Alocale’s full weekday name (e.g., Sunday)
%blocale’s abbreviated month name (e.g., Jan)
%Blocale’s full month name (e.g., January)
%clocale’s date and time (e.g., Thu Mar 3 23:05:25 2005)
%Ccentury; 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
%Hhour (00..23)
%Ihour (01..12)
%jday of year (001..366)
%khour ( 0..23)
%lhour ( 1..12)
%mmonth (01..12)
%Mminute (00..59)
%plocale’s equivalent of either AM or PM
%rlocale’s 12-hour clock time (e.g., 11:11:04 PM)
%R24-hour hour and minute; same as %H:%M
%Ssecond (00..60)
%Ttime; same as %H:%M:%S
%ylast two digits of year (00..99)
%Yyear

For additional formatting you can use the following characters after the % characters in each variable above.

  • – (hyphen) do not pad the field
  • _ (underscore) pad with spaces
  • 0 (zero) pad with zeros
  • ^ use upper case if possible
  • # use opposite case if possible

Here are some more date command examples using the settings above as-well as the extra formatting.

# Uppercase Month
date +"%^B %d %Y"
OCTOBER 14 2017

# Shortened Month
date +"%b-%d-%Y"
Oct-14-2017

# Short Digits Only Output
date +"%m-%d-%y"
10-14-17

Set Time And Date On Linux

You can also set the date and time using the Linux date command, although you will require root or sudo permissions to do so. Here are 2 examples showing you how to change the date and time on your Linux desktop or server.

date -s "Monday October 16 13:57:00 EST 2017"
date -s "10/14/2017 17:41"

If you only want to change the time but not the date, you can do so as-well. Here are 3 examples of how to set the time below.

date -s "13:57:00 EST"
date -s "13:57"
date -s "1:57 PM EST"

I hope this quick guide helped, please don’t forget to like/comment/share. Thanks for the support!

Leave a Reply

Your email address will not be published. Required fields are marked *