How to Use Date Format in Bash [5 Examples]

The date command in Bash offers multiple different formats for printing dates in different ways. This article provides you with an extensive list of all the available formatting options while using the date command. Along with the list, I will show you a few examples to demonstrate how to use a date format in Bash to achieve the desired result.

Key Takeaways

  • Learning about various format specifiers.
  • Implementing those specifiers in practical Bash script examples.

Free Downloads

Different Formatting Options

The date command prints the current date and time in the following format by default.

Weekday_name Month_name Day_of_month Hour:Minutes:Seconds AM/PM Timezone(HH) Year. For example, Tue Jul 11 12:14:01 PM +06 2023

See the following output, when the date command is run in the Ubuntu terminal.Executing the date commandTo achieve similar output manually one can use the following command with different date formats.

date "+%a %b %d %I:%M:%S %p %:::z %Y

Note: Place a plus sign (+) before the formatting options. If multiple formatting options are in action, put them within quotes.

To know more about each formatting option of the command and other available formatting options go through this list.

5 Practical Examples of Bash Date Format

At this point, I hope you are quite familiar with different formatting options. Now, I am going to show how to use those options and print the date in the required formatting.

Example 1: Printing Date in YYYY-MM-DD Format

This first example is for making you more familiar with the format specifiers such as %Y and %d. These format specifiers are useful for printing dates in YYYY-MM-DD format.

Steps to Follow >

❶ At first, launch an Ubuntu Terminal.

❷ Write the following command to open a file in the built-in nano editor:

nano format1.sh

EXPLANATION
  • nano: Opens a file in the Nano text editor.
  • format1.sh: Name of the file.

Creating a .sh file in nano editor❸ Copy the following script and paste it into nano. Press CTRL+O and ENTER to save the file; CTRL+X to exit. Alternatively, copy the following script. Paste the script in a text editor and save it as .sh file.

#!/bin/bash

today=$(date +%Y-%m-%d)
echo $today

EXPLANATION

The script utilizes the format “%Y” to retrieve the full year (YYYY), “%m” for the month (MM) and “%d” for the day (DD). After substituting the date command in the variable “today“, it outputs the value of the variable using the echo command.

❹ Use the following command to make the file executable:

chmod u+x format1.sh

EXPLANATION
  • chmod: Changes permissions. 
  • u+x: Giving the owner executing permission. 
  • format1.sh: Name of the script. 

Changing permission of a Bash scriptRun the script by the following command:

./format1.sh

Once you run the script, it prints the date 2023-07-11 in the expected format.

Note: Instead of using multiple format specifier, you can use %F format to print date in YYYY-MM-DD format

Example 2:  Printing Date in YY-MM-DD  Format

In this example, I am going to run a Bash script that will ultimately print the date in YY-MM-DD format. 

You can follow the Steps of Example 1 to learn about creating and saving shell scripts.

Scripts (format2.sh) >

#!/bin/bash

today=$(date +%y-%m-%d)
echo $today

EXPLANATION

The script utilizes the format “%y” to retrieve year (YY), “%m” for the month (MM) and “%d” for the day (DD). After substituting the date command in the variable “today“, it outputs the value of the variable using the echo command.

Run the script by the following command:

./format2.sh

YY-MM-DD format of dateThe program prints the current date 23-07-11 as specified by the format specifiers.

Example 3: Bash Date Format Weekday, Month Day, Year

Now, I want to print the date in a customized format where weekday and year should be separated by a comma (,). You can also achieve this using format specifiers of the following script. 

You can follow the Steps of Example 1 to learn about creating and saving shell scripts.

Scripts (format3.sh) >

#!/bin/bash

today=$(date “+%A, %B-%d, %Y”)
echo $today

EXPLANATION

The script specifies the format “%A” to get the full name of the weekday. Then “%B” and %d to get the full name of the month and day of the month separated by a hyphen (-).

Finally, to get the full year (YYYY) it uses the format specifiers “%Y“. After substituting the date command in the variable “today“, it outputs the value of the variable using the echo command.

Run the script by the following command:

./format3.sh

Printing full name of monthAfter executing the script, it displays the current date in the following format: Tuesday, July-11, 2023. The output includes the weekday, the month with the day of the month, and the year, with each item separated by a comma.

Example 4: Print the Day of the Current Week

Using the format specifiers of the date command you can print the day of the current week. The  %u format specifier starts counting days of the current week from Monday. So, Monday is day 1 of the current week.

You can follow the Steps of Example 1 to learn about creating and saving shell scripts.

Scripts (format4.sh) >

#!/bin/bash

today=$(date +%u)
echo “Day of the week is: $today”

EXPLANATION

The script utilizes “%u” to get the day of the current week. After substituting the date command in the variable “today“, it outputs the value of the variable using the echo command.

Run the script by the following command:

./format4.sh

Printing day the week using date formatAfter executing the script, it shows the Day of the week is 2. As the “%u” format specifier starts counting days of the week from Monday, 2 means Tuesday.

Example 5: Bash Date in 24-Hour Format

While printing date someone may stumble in printing time in 24-hour format. Here is how you can use the date command to print time in 24-hour format. 

You can follow the Steps of Example 1 to learn about creating and saving shell scripts.

Scripts (format5.sh) >

#!/bin/bash

c_time=$(date +%H:%M:%S)
echo $c_time

EXPLANATION

The script utilizes the format “%H” to retrieve the current hour in 24-hour format, “%M” for minutes and “%S” for running second. After substituting the date command in the variable “c_time“, it outputs the variable’s value using the echo command.

Run the script by the following command:

./format5.sh

24 hour time format in BashWhen executing the program shows the current time 17:25:14 in a 24-hour time format.

Date Formatting Options

Apart from the examples in this article, you can achieve desired date format using the wide variety of format specifiers given in the table below:

Format Description Example code Sample Output
%% A literal $ date +%% %
%a Locale’s weekday name in shorter form; Such as Sun $ date +%a Sun
%A Locale’s full weekday name; Such as Sunday $ date +%A Sunday
%b Locale’s month name in shorter form $ date +%b Jun
%B Locale’s full month name $ date +%B June
%c Locale’s date and time $ date +%c Sun 18 Jun 2023 12:18:40 PM +06
%C century $ date +%C 20
%e Day of month $ date +%e 18
%F Full date; same as %Y-%m-%d $ date +%F 2023-06-18
%g Last two digits of the year of ISO week number $ date +%g 23
%G year of ISO week number $ date +%G 2023
%d Day of month $ date +%d 18
%D Date; same as %m/%d/%y $ date +%D 06/18/23
%H Hour $ date +%H 11
%I Hour $ date +%I 11
%j Day of year $ date +%j 169
%m Month of year $ date +%m 06
%M Minute(00 to 59)  $ date +%M 32
%r Locale’s 12-hour clock time $ date +%r 11:12:14 AM
%q Quarter of a year $ date +%q 2
%s seconds since 1970-01-01 00:00:00 UTC $ date +%s 1687068749
%S seconds(00 to 60) $ date +%S 50
%t A tab $ date +%t
%T Time as same as %H:%M:%S $ date +%T 12:10:13
%u Day of week(1 to 7); 1 is Monday $ date +%u 7
%U Week number of the year(0 to 53); Sunday as first day of week $ date +%U 25
%V ISO week number of the year(0 to 53); Monday as the first day of week $ date +%V 24
%w Day of week(0 to 6); 0 is sunday $ date +%w 0
%W Week of year(0 to 53); Monday as firstday of the week $ date +%W 24
%x Locale’s date representation $ date +%x 06/18/2023
%X Locale’s time representation $ date +%X 12:04:10 PM
%y Last two digits of year $ date +%y 23
%Y Full year $ date +%Y 23
%z +hhmm numeric time zone $ date +%z +0600
%:z +hh:mm numeric time zone $ date +%:z +06:00
%::z +hh:mm:ss numeric time zone $ date +%::z +06:00:00
%:::z numeric time zone with : to necessary precision(+06, -05:30) $ date +%:::z +06
%Z Time zone abbreviation  $ date +%Z +06

Conclusion

In conclusion, the date command in Bash is useful for printing dates and times. One can use different format specifiers along with the date command to display or format the date as per requirement.

People Also Ask

How to get date of a day before the current date?
To get the date of a day before, use “yesterday” or “1 day ago” with the date command.
How to increment a day in Bash script?
To increment a day from a specific date use +1 day with the date command along with the -d option. 
Is placing the format specifiers within quotes in mandatory?
Spoiler title
No. If you use single format specifiers you don’t need to place the specifier within quotes. However, when you use multiple specifiers at a time you need to put those specifiers within a single or double quote.

Related Articles


<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial

Rate this post
Md Zahidul Islam Laku

Hey, I'm Zahidul Islam Laku currently working as a Linux Content Developer Executive at SOFTEKO. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). I write articles on a variety of tech topics including Linux. Learning and writing on Linux is nothing but fun as it gives me more power on my machine. What can be more efficient than interacting with the Operating System without Graphical User Interface! Read Full Bio

Leave a Comment