JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Get Current Date and Time in Bash

Author:JIYIK Last Updated:2025/03/22 Views:

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


Using the Date Command in Bash

An external Bash program called the date command can change or display the system time and date. In addition, it provides a variety of format choices.

By default, every Linux distribution includes the date command. The syntax of the date command is as follows:

date +[format_option]

If we just type the date command with no format options, it will display the complete date and time in a verbose format.

Let's look at the output below:

date Command Output

Additionally, many formatting options can be used with the date command depending on your requirements.


Format options in the date command

Following is a list of format options available for the date command:

Format describe Output
date +%a This format gives the abbreviated name of the current weekday. Monday, Tuesday, Friday
date +%A This format gives the full name of the current weekday. Monday Tuesday
date +%b This format gives the abbreviated name of the current month. January, March, April
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 24-hour format. 21
date +%I This format displays the current hour in 12-hour format. 11
date +%j This format displays the current day of the year. 001-365
date +%m This format displays the number of the current month. 01-12
date +%M This format displays the current minute. 00-59
date +%S This format displays the current second. 00-59
date +%T This format displays the current time in 24-hour format. 17:54:32
date +%u This format shows the current day of the week. One is Monday. 1-7
date +%U This format displays 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. Greenwich Mean Time, IST

These formats can be used to get the date according to the format you require. Let’s see some examples of displaying the date in different formats.


Display date in MM/DD/YYYY format

The following script will display the date in mm-dd-yyyy format. Note that it is case sensitive because uppercase letters have different meanings than lowercase 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 date in MM-YYYY format

It is not mandatory to display year, month and day. You can skip any of them based on your requirement.

The following Bash script will display only the month and year in 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

It is also possible to omit punctuation used as separators in dates. To do this, you can use the following script:

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

This will give the following output:

Date script that outputs without punctuation


Summarize

Date is an inbuilt program in all Unix-like operating systems and apart from displaying the current date, it can also be used with other commands. In this article, we have learned how to use the date command of Bash script and its syntax to display data in various formats.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Print a file after skipping first X lines in Bash

Publish Date:2025/03/22 Views:186 Category:OPERATING SYSTEM

Suppose you have a file, a large file, and you want to display its contents. How would you do it? You obviously don't want to print out the entire contents of the file, as that's not very practical. You might want to print some selective li

Bash History Size

Publish Date:2025/03/22 Views:189 Category:OPERATING SYSTEM

In this article, we will learn about Bash history, its size, and how we can change our history size and handle limits. Before getting into our topic, let us first understand why we need history in Bash shell and how we can get it. Most deve

Find Current Folder Name in Bash

Publish Date:2025/03/22 Views:106 Category:OPERATING SYSTEM

Finding a directory is very easy through Bash scripting. But finding the exact directory folder name you are in right now is a bit complicated. This article will introduce three methods to find the folder name from this article directory. I

Changing Directories in Bash

Publish Date:2025/03/22 Views:75 Category:OPERATING SYSTEM

In this article, we will learn how to change directories in Bash. The term directory is used to refer to a folder. Because you frequently move between folders when using Bash and when using the Git version control system, it is essential to

Running Regular Expressions in Case Statements in Bash

Publish Date:2025/03/22 Views:75 Category:OPERATING SYSTEM

This article explores regular expressions, their basic syntax, and how to run them in Bash using case and if-else constructs. Introduction to Regular Expressions A regular expression, also called regex or regexp, is a sequence of characters

Sending a message to logged in users in Bash

Publish Date:2025/03/22 Views:168 Category:OPERATING SYSTEM

This article explores methods to send data to another logged in user in Bash. This article discusses methods to find active users and send them messages. Finding Online Users in Bash Before sending data to an online user, you must verify wh

Bash copy all files from remote directory

Publish Date:2025/03/22 Views:156 Category:OPERATING SYSTEM

This article describes methods for copying files between different hosts on a network. It also explores the scp command and how to use it to copy files from a remote location. scp command Secure Copy Protocol (SCP) is a secure copy network

Floating-point arithmetic in Bash

Publish Date:2025/03/22 Views:189 Category:OPERATING SYSTEM

This short article describes how to quickly perform floating-point calculations in GNU BASH (shell scripts) directly at the command prompt or in a shell script. If you work with numbers, it can be helpful to perform quick floating-point cal

Writing to a File in Bash

Publish Date:2025/03/22 Views:133 Category:OPERATING SYSTEM

This article will let us explore different ways to write files in bash. Files can be appended or overwritten as required. Let's see how we can do it. Different ways to write/overwrite files in Bash We will see several operators, such as and

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial