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= StringIt is used to display the time set by the String value.
-s, -set=StringIt is used to set the time set by the String value.
-f or -file=DateFileIt 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=FileIt is used to print the last modification time of a file.
-u, -utc, -universalIt is used to print or set Coordinated Universal Time.
-helpIt is used for getting the help of this command.
-versionIt is used to get the version information.

Format Option Codes

Format Option with CodesPart of DateDescriptionExample Output
date +%aWeekdayName of a weekday in short form (e.g., Sun, Mon, Tue, Wed, Thu, Fri, Sat
Mon
date +%AWeekdayName of the weekday in full form (e.g., Sunday, Monday, Tuesday, etc.)
Monday
date +%bMonthName of the month in short form (e.g., Jan, Feb, Mar, etc.)
Jan
date +%BMonthName of the month in full form (e.g., January, February, etc.)
January
date +%dDayDay of the month (e.g., 01)
27
date +%DMM/DD/YYCurrent Date; shown in MM/DD/YY
08/27/2019
date +%FYYYY-MM-DDDate; shown in YYYY-MM-DD
2019-08-27
date +%HHourHour in 24-hour clock format
16
date +%IHourHour in 12-hour clock format
04
date +%jDayDay of year (e.g., 001 to 366)
239
date +%mMonthNumber of month (01 to 12 where 01 is January)
08
date +%MMinutesMinutes (00 to 59)
55
date +%SSecondsSeconds (00 to 59)
28
date +%NNanosecondsNanoseconds (000000000 to 999999999)
300261496
date +%THH:MM:SSTime as HH:MM:SS (Hours in 24 Format)
15:59:10
date +%uDay of WeekDay of week (01 to 07 where 01 is Monday)
02
date +%UWeekDisplays week number of year where Sunday is the first day of the week (00 to 53)
35
date +%YYearDisplays full year (i.e., YYYY)
2019
date +%ZTimezoneTime 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