Get Current Date and Time in Bash
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:
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:
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:
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:
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.
Related Articles
How to get the time difference in minutes in PHP
Publish Date:2025/04/12 Views:83 Category:PHP
-
In this article, we will describe the method of getting the time difference in minutes in PHP. Using date_diff() Functions Using Mathematical Formulas Use the function in PHP date_diff() to get the time difference in minutes We will use the
Convert Pandas Series Datetime to String in Python
Publish Date:2025/04/12 Views:60 Category:Python
-
Pandas Series is a one-dimensional array that can hold any data type and label. Suppose you have a Pandas Series of String datetime objects. We can convert a String object to its string equivalent using strftime() the String function and so
Subtract one day from a timestamp date in PostgreSQL
Publish Date:2025/04/09 Views:183 Category:PostgreSQL
-
Before we begin, let's define what a timestamp is in SQL. From the PostgreSQL documentation under the DATE/TIME heading, a timestamp is a data type that stores a date and time in the following format. YYYY-MM-DD hh:mm:ss (DATE | TIME) The s
PostgreSQL Extract date from timestamp
Publish Date:2025/04/09 Views:148 Category:PostgreSQL
-
PostgreSQL provides many functions and operators for built-in data types. This tutorial will give an example of extracting the date from a timestamp in PostgreSQL or Postgres. CAST Extracting the date from a timestamp using the operator in
Move Multiple Files in Linux Bash
Publish Date:2025/04/06 Views:97 Category:OPERATING SYSTEM
-
In this article, we will explain how to move multiple files to the same directory in Linux. We will explain different methods such as typing multiple file names, using wildcard characters ( * ) to represent similar file names and/or identic
How to Compare Strings in Bash
Publish Date:2025/04/06 Views:190 Category:OPERATING SYSTEM
-
We can compare strings using various comparison operators and check if a string contains a substring using regular expressions. String comparison in Bash String comparison means checking whether the given strings are identical or not. Two o
How to Add Comments in Bash Scripts
Publish Date:2025/04/06 Views:123 Category:OPERATING SYSTEM
-
Comments are lines that are ignored by the interpreter and are only used to describe what is happening in the code or to provide insight into a particular block or line of code. Comments make it easier for the reader to understand the code.
tr Command in Linux Bash
Publish Date:2025/04/05 Views:54 Category:OPERATING SYSTEM
-
In Linux, we can use Bash scripts to perform string manipulation such as concatenation, truncation, and finding words in a text. tr This article will explain how to translate or remove characters using command in Linux Bash . tr Using comma
Differences between Bash Profile and Bashrc
Publish Date:2025/04/05 Views:188 Category:OPERATING SYSTEM
-
This article explains the difference between ~./bash_profile and files in Bash. ~/.bashrc What are startup files in Bash? Startup files are files that are executed after the shell is started. The startup files depend on the type of shell th