date resides in the Bash shell, which is common in most Linux distributions and even MacOS is pre-set. This tutorial will show you how to use
date on the command line and how to use it in shell scripts to print more than just the time.
Run the command
date see this information. It outputs the current date and time for your time zone:
The default formatting looks a bit silly. Why is not the year printed after the month and day, but displayed behind the time zone at the end? Do not be afraid: If you control the format of the desired output,
date returns it in spades. There are more than 40 options that you can pass to
date to get the instruction to format the output exactly as you like.
To use one of the options of type
date a space, a plus sign
+ and the option including the leading percent sign. The option
% c (date and time in the locale format) causes the date and time to be printed in the normalized format associated with the locale. Your locale will be determined by the geographic and cultural information you provided when you installed your operating system. The locale governs things like the currency symbol, paper sizes, time zones, and other cultural norms.
Date +% c
The year now appears in a more natural position in the output.
You can pass multiple options to
date at the same time. A sequence of options is called a format string. Use this option to display the name of the day (
% A ), the day of the month (
% d ), and the month name (
% B ) command :
Date +% A% d% B
That worked, but it's ugly. No problem, we can include spaces as long as we enclose the entire format string in quotation marks. Note that
+ is outside the quotation marks.
Date + "% A% d% B"
You can add text to the format string, for example:
Date + "Today is :% A% d% B "
 Scrolling up and down through the man page
date in search of the desired option will soon be tedious. We've grouped the options together to help you find them.
Date and time options
- % c : Prints the date and time in the format of your locale. including the time zone.
Date display options
- % D : Prints the date in mm / dd / yy format.
- % F : Prints date in the format YYYY-MM-DD.
- % x : Prints the date in the format of your locale.
Options for displaying the day % a : Prints the name of the day, abbreviated to Mo, Di, Mi, and so on.
Options Display of the week
- % U : Prints the week number of the year and considers Sunday as the first day of the week. For example, the third week of the year, the twentieth week of the year, and so on.
- % V : Returns the ISO week number of the year, with Monday being the first day of the week.
- % W : Week number of the year, with Monday being the first day of the week.
Options for Displaying the Month
- % b or % h : Prints the name of the month with the abbreviation Jan, Feb, Mar, etc
- % B : Prints the full name of the month, January, February, March, etc. % m : Prints the month's number with a leading zero, if necessary, 01, 02, 03 … 12.
Display year options
- % C : Prints the century without year. In 2019 it would print 20.
- % y : Prints the year in double digits. 1919 will be printed in 2019.
- % Y : Returns the year in four digits.
Time display options
- % T : Prints the time as HH: MM: SS.
- % R : Returns the time Hours and minutes as HH: MM without seconds in 24-hour format off.
- % r : Prints the time according to the locale based on the 12-hour clock and an AM or PM indicator.
- % X : Prints the time in 24-hour format according to your locale. Allegedly Note that during the test, this option behaved exactly like
% ras shown below. As expected, on a Linux computer configured for the UK locale and set to GMT, he printed the time in 24-hour format without AM or PM.
<img class = "alignnone size-full wp-image-410505" data-pagespeed-lazy-src = "https://www.howtogeek.com/wp-content/uploads/2019/04/xHTG_011.png .pagespeed.gp + jp + jw + pj + ws + js + rj + rp + rw + ri + cp + md.ic.d0ZLZM1WIO.png "alt =" Output the date command with TR r X options  Options for Hour display
- % H : Prints the hours 00, 01, 02 … 23.
- % I : Prints the hours with the 12-hour clock 00, 01, 02 … 12, leading zero, if any.
Minutes display options
- % M : Prints the minutes, 01, 02, 03 … 59, with if necessary, a leading zero.
Options for displaying seconds
- % s : Prints the number of seconds since 1970-01-01 00:00 : 00. the beginning of the Unix epoch.
- % S : Prints the seconds 01, 02, 03 … 59 with a leading zero, fall s required.
- % N : Prints the nanoseconds.  Output of the date command with s SN options  Options for displaying time zone information
- % z : Prints the time difference between your time zone and your UTC time.
- %: z : Prints the time difference between your time zone and your UTC time: between hours and minutes. Note the
- % :: z : Prints the time difference between your time zone and UTC with a: between the hours, minutes, and seconds. Note the
- % Z : Outputs the alphabetical time zone name.
- % p : Prints the AM or PM indicator in uppercase.
- % P : The AM or PM indicator is printed in lowercase. Note the special features of these two options. A lowercase
puppercase letters, uppercase letters
Pare lowercase letters.
- % t : Prints a tab.
- % n : Prints a new line.
Options for modifying other options
These modifiers can be pasted between
%and the option letter of other options to display them to change. For example,
% - Swould remove the leading zero for single-digit seconds.
- – : A single hyphen prevents zeros from single-digit values.
- _ : A single underscore adds leading digits for single-digit values.
- 0 : Represents leading zeros for single-digit values.
- ^ : Use capital letters if possible (not all options take this modifier into consideration).
- # : Try to use the opposite of the option's default setting (not all options apply to this modifier.)
Two more tricks  To get the last modification time of a file, use the option
-r(reference). Note that here a
-(hyphen) character is used instead of a
%character and no
+symbol is required. Try the following command in your home folder:
date -r .bashrc
The TZ setting allows you to set your time zone for the duration of a file Single change command.
TZ = GMT date +% c
Using the date in scripts
Enabling a bash shell script to print time and date is trivial. Create a text file with the following content and save it as
#! / Bin / bash TODAY = $ (date + "Today is% A,% d of% B") TIMENOW = $ (date + "local time is% r") TIME_UK = $ (TZ = BST date + "UK time is% r") Echo $ TODAY echo $ TIMENOW echo $ TIME_UK
Enter the following command to set the execute permissions and make the script executable:
chmod + x gd.sh
Run the script with this command:
./ gd.sh 
We can use the Date command to provide a timestamp. The displayed script creates a directory with the timestamp as name. All text files from the current folder will be copied to the folder. If you run this script regularly, we can take a snapshot of our text files. Over time, we build a bunch of folders with different versions of our text files.
Note that this is not a robust backup system, just illustrative.
Create a text file with the following content and save it as
#! / bin / bash # Date and time received date_stamp = $ (date + "% F-% H-% M-% S") # Create a directory with this name mkdir "$ date_stamp" # Copy the files from the current folder cp * .txt "$ date_stamp" # everything ready, report and finish echo "Text files copied to the directory:" $ date_stamp
Enter the following command to set the execute permissions and make the script executable.
chmod + x snapshot.sh
Run the script with this command:  ./ snapshot.sh
them see that a directory has been created. Its name is the date and time the script was run. This directory contains copies of the text files.
With a little thought and creativity, even the modest date