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.
|%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|
|%j||day of year (001..366)|
|%k||hour ( 0..23)|
|%l||hour ( 1..12)|
|%p||locale’s equivalent of either AM or PM|
|%r||locale’s 12-hour clock time (e.g., 11:11:04 PM)|
|%R||24-hour hour and minute; same as %H:%M|
|%T||time; same as %H:%M:%S|
|%y||last two digits of year (00..99)|
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!