How to Get Current Date and Time in Bash

Naila Saad Siddiqui Feb 15, 2024
  1. Use the date Command in Bash
  2. Format Options in the date Command
  3. Display the Date in the MM/DD/YYYY Format
  4. Display the Date in the MM-YYYY Format
  5. Display the Date and Time Without Any Punctuation Marks
  6. Conclusion
How to Get Current Date and Time in Bash

This article discusses the method to display the current date and time in some specified format in Bash Scripting. For this purpose, the date command is used with multiple options.

Use the date Command in Bash

An external Bash program called the date command makes it possible to change or display the system time and date. Additionally, it offers numerous formatting choices.

Every Linux distribution includes the date command by default. The syntax of the date command is as follows:

date +[format_option]

If we type only the date command without the format options, it displays the complete date and time in a detailed format.

Let’s look at the output below:

Date Command Output

Moreover, numerous format options can be used with the date command per your requirements.

Format Options in the date Command

Following is the list of format options that are available with the date command:

Format Description Output
date +%a This format gives the short name of the current weekday. Mon, Tue, Fri
date +%A This format gives the full name of the current weekday. Monday, Tuesday
date +%b This format gives the short name of the current month. Jan, Mar, Apr
date +%B This format gives the full name of the current month. January, March
date +%d This format displays the current day of the month. 09
date +%D This format displays the current date in MM-DD-YY format. 10-08-2022
date +%F This format displays the current date in YYYY-MM-DD format. 2022-10-08
date +%H This format displays the current hour in a 24-hour format. 21
date +%I This format displays the current hour in a 12-hour format. 11
date +%j This format displays the current day of the year. 001-365
date +%m This format displays the current number of the month. 01-12
date +%M This format displays the current minutes. 00-59
date +%S This format displays the current seconds. 00-59
date +%T This format displays the current time in 24-hour format. 17:54:32
date +%u This format displays the current number of the day of the week. One is Monday. 1-7
date +%U This format shows the current week number of the year. 00-53
date +%Y This format displays the current year. 2022
date +%Z This format displays the current time zone. GMT, IST

These formats can be used to get the date per your required format. Let us look at some examples to display the date in different formats.

Display the Date in the MM/DD/YYYY Format

The following script will display the date in the mm-dd-yyyy format. Be careful about case sensitivity, as capital letters mean different than small letters.

#!/bin/bash
curr_date=`date +%m/%d/%Y`
echo $curr_date

This will display the following output:

Date Script MM-DD-YYYY Output

Display the Date in the MM-YYYY Format

It is not mandatory to display year, month, and date. You can skip any of these based on your requirements.

The following Bash script will display only the month and year in the MM-YYYY format:

#!/bin/bash
curr_date=`date +%m-%Y`
echo $curr_date

This will give the following output:

Date Script MM-YYYY Output

Display the Date and Time Without Any Punctuation Marks

It is also possible to omit the punctuation marks used as separators in date. For that, the following script can be used:

#!/bin/bash
curr_date=`date +%Y%m%d%H%M%S`
echo $curr_date

This will give the following output:

Date Script Without Punctuation Marks Output

Conclusion

The date is a built-in program in all Unix-like operating systems, which can be used with other commands in addition to displaying the current date. In this article, we learned how to use Bash scripting’s date command and its syntax to display data in various formats.

Related Article - Bash DateTime