Home » Bash Date Format

Bash Date Formatting

In this topic, we will learn about available format options for date command and understand how can we use them with Bash Scripts.

Bash Date

Bash shell provides different date commands along with different formatting options. We can use those commands to format Bash date to a required one.

Bash Date Command

We can use the `date` command to display or change the current date and time value of the system. We can print date and time value in different formats by using date command. We can also use this command for calculating date and time value-related tasks. If `date` command is used without any option, then it will print the current system’s date and time value. This command contains several formatting options to format the output.

The syntax of date command is given below:

Format Bash Date with Options

As we have discussed above, we can format the Bash Date. We can also use spaces with the format that you are going to use.

Date command accepts the options if provided like:

If we want to format the date with spaces, we can use the syntax:

List of Bash Date Formatting-options

There are different types of formatting codes or characters available, which can be used with the date options to generate the formatted output. Following is a list of some common options and format codes for date command:

Options:

-d or -date= String It is used to display the time set by the String value.
-s, -set=String It is used to set the time set by the String value.
-f or -file=DateFile It is used to process multiple dates.
-I or -iso-8601[=Timespec] It is used to generate an ISO 8601 compliant date/time string output.
-r or -reference=File It is used to print the last modification time of a file.
-u, -utc, -universal It is used to print or set Coordinated Universal Time.
-help It is used for getting the help of this command.
-version It is used to get the version information.

Format Option Codes

Format Option with Codes Part of Date Description Example Output
date +%a Weekday Name of a weekday in short form (e.g., Sun, Mon, Tue, Wed, Thu, Fri, Sat
Mon
date +%A Weekday Name of the weekday in full form (e.g., Sunday, Monday, Tuesday, etc.)
Monday
date +%b Month Name of the month in short form (e.g., Jan, Feb, Mar, etc.)
Jan
date +%B Month Name of the month in full form (e.g., January, February, etc.)
January
date +%d Day Day of the month (e.g., 01)
27
date +%D MM/DD/YY Current Date; shown in MM/DD/YY
08/27/2019
date +%F YYYY-MM-DD Date; shown in YYYY-MM-DD
2019-08-27
date +%H Hour Hour in 24-hour clock format
16
date +%I Hour Hour in 12-hour clock format
04
date +%j Day Day of year (e.g., 001 to 366)
239
date +%m Month Number of month (01 to 12 where 01 is January)
08
date +%M Minutes Minutes (00 to 59)
55
date +%S Seconds Seconds (00 to 59)
28
date +%N Nanoseconds Nanoseconds (000000000 to 999999999)
300261496
date +%T HH:MM:SS Time as HH:MM:SS (Hours in 24 Format)
15:59:10
date +%u Day of Week Day of week (01 to 07 where 01 is Monday)
02
date +%U Week Displays week number of year where Sunday is the first day of the week (00 to 53)
35
date +%Y Year Displays full year (i.e., YYYY)
2019
date +%Z Timezone Time zone abbreviation (e.g., IST, GMT)
GMT

We can use any of the formats as mentioned above (first column) for the date command as per requirement.

Examples

Bash Date Format MM-DD-YYYY

To use the date in MM-DD-YYYY format, we can use the command date +%m-%d-%Y.

Bash Script Program

Bash Console View

Bash Date Formatting

Output

Bash Date Formatting

It is very important to note that format option codes are case sensitive. In this example, we have used %m for a month, %d for a day and %Y for a year. If we had used %M in place of %m, then it would define minutes.

Bash Date Format MM-YYYY

To use the date in MM-YYYY format, we can use the command date +%m-%Y.

Bash Script Program

Bash Console View

Bash Date Formatting

Output

Bash Date Formatting

Bash Date Format Weekday DD-Month-YYYY

To use the date in Weekday DD-Month, YYYY format, we can use the command date +%A %d-%B, %Y.

Bash Script Program

Bash Console View

Bash Date Formatting

Output

Bash Date Formatting

Conclusion

In this topic, we discussed available date format options and some example demonstrating the usage of them.


Next TopicBash Sleep

You may also like