In Bash shell scripting, the date command is commonly used to print the current date and time, create timestamped file names, generate log entries, and format dates for reports or automation scripts. This tutorial explains the most useful Bash date format options, how to combine them, and how to avoid common mistakes such as using %M when you need the month.

Bash date command for printing the current date and time

To display the current system date and time in Bash, run the date command without any format string. The exact output depends on your system locale, time zone, and operating system.

Bash date command syntax without formatting

Following is the syntax of date command

</>
Copy
$ date

A typical output may look like the following.

Sat Dec 30 11:55:39 IST 2017

Bash date format syntax with percent specifiers

To format Bash Date to a required one, bash shell provides date command along with many format options.

Date command accepts options if provided any

</>
Copy
$ date +<format-option><format-option>

The format string starts with a plus sign +. Each date or time part is represented by a percent specifier such as %Y for four-digit year, %m for month number, and %d for day of month.

To format date with spaces, use the syntax

</>
Copy
$ date '+<format-option><format-option> <format-option>'

Quote the format string when it contains spaces, punctuation that may be interpreted by the shell, or text mixed with date specifiers. Single quotes are usually preferred because they prevent the shell from expanding special characters before date receives the format.

</>
Copy
date '+Today is %A, %d %B %Y'
Today is Saturday, 30 December 2017

Useful Bash date formatting options and examples

Following are the list of available options for date command :

Format optionPart of DateDescriptionExample Output
date +%aWeekdayName of weekday in short (like Sun, Mon, Tue, Wed, Thu, Fri, Sat)Mon
date +%AWeekdayName of weekday in full (like Sunday, Monday, Tuesday)Monday
date +%bMonthName of Month in short (like Jan, Feb, Mar )Jan
date +%BMonthMonth name in full (like January, February)January
date +%dDayDay of month (e.g., 01)04
date +%DMM/DD/YYCurrent Date; shown in MM/DD/YY02/18/18
date +%FYYYY-MM-DDDate; shown in YYYY-MM-DD2018-01-19
date +%HHourHour in 24-hour clock format18
date +%IHourHour in 12-hour clock format10
date +%jDayDay of year (001..366)152
date +%mMonthNumber of month (01..12) (01 is January)05
date +%MMinutesMinutes (00..59)52
date +%SSecondsSeconds (00..59)18
date +%NNanosecondsNanoseconds (000000000..999999999)300231695
date +%THH:MM:SSTime as HH:MM:SS (Hours in 24 Format)18:55:42
date +%uDay of WeekDay of week (1..7); 1 is Monday7
date +%UWeekDisplays week number of year, with Sunday as first day of week (00..53)23
date +%YYearDisplays full year i.e. YYYY2018
date +%Z TimezoneTime zone abbreviation (Ex: IST, GMT)IST

You may use any of the above-mentioned format options (first column) for the date command in the aforementioned syntax.

Here are a few additional format options that are useful in scripts, especially for timestamps, ISO-style output, and portable log names.

Format optionMeaningExample outputCommon use
date +%yYear without century17Short dates
date +%eDay of month padded with a space 4Human-readable reports
date +%RHour and minute in 24-hour time18:55Short time display
date +%r12-hour clock time with AM/PM06:55:42 PMHuman-readable time
date +%sSeconds since Unix epoch1514615139Time comparison in scripts
date +%zNumeric time zone offset+0530Log timestamps
date +%VISO week number52Week-based reports
date +%cLocale date and timeSat 30 Dec 2017 11:55:39 AM ISTReadable local output

Bash date format examples for common date patterns

Bash date format MM-DD-YYYY

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

Bash Script

</>
Copy
#!/bin/bash

d=`date +%m-%d-%Y`
echo $d    # 12-30-2017

Output

11-20-2020

Please observe the upper and lower case letters : %m for month, %d for day and %Y for year. %M would mean minutes.

For example, if the command is run on December 30, 2017, the output will be as follows.

12-30-2017

Bash date format YYYY-MM-DD with %F

The %F option prints the date in YYYY-MM-DD format. It is a convenient shortcut for %Y-%m-%d and is useful for filenames because the values sort naturally in chronological order.

</>
Copy
today=$(date +%F)
echo "$today"
2017-12-30

You can also write the same format explicitly.

</>
Copy
date +%Y-%m-%d

Bash date format MM-YYYY

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

Bash Script

</>
Copy
#!/bin/bash

d=`date +%m-%Y`

echo $d    # 12-2017

Output

11-20-2020

For MM-YYYY, the output contains only the month and four-digit year. For example:

12-2017

Bash date format with weekday, day, month name, and year

To format a date with the full weekday name, day of month, full month name, and four-digit year, combine %A, %d, %B, and %Y. Because this format contains spaces, place the format string inside quotes.

Bash Script

</>
Copy
#!/bin/bash

d=`date '+%A %d-%B, %Y'`

echo $d   #  Saturday 30-December, 2017

Output

11-20-2020

For a run date of December 30, 2017, the formatted output will look like this:

Saturday 30-December, 2017

Bash date format for timestamped filenames and logs

In scripts, date formatting is often used to create filenames that do not overwrite previous files. A compact timestamp such as YYYYMMDD_HHMMSS works well because it is readable and sorts correctly.

</>
Copy
timestamp=$(date +%Y%m%d_%H%M%S)
backup_file="backup_${timestamp}.tar.gz"

echo "$backup_file"
backup_20171230_115539.tar.gz

For log files, it is better to include a time zone offset when the logs may be read from another server or region.

</>
Copy
date '+%Y-%m-%d %H:%M:%S %z'
2017-12-30 11:55:39 +0530

Bash date command with a specific date string

On GNU/Linux systems, you can format a specific date using the -d option. This is useful when you want to print yesterday, tomorrow, next week, or a fixed date in a chosen format.

</>
Copy
date -d '2017-12-30' '+%A, %d %B %Y'
Saturday, 30 December 2017

Relative dates can also be used on GNU/Linux systems.

</>
Copy
date -d 'yesterday' +%F
date -d 'tomorrow' +%F
date -d 'next Monday' '+%F %A'

Note that macOS/BSD date does not use GNU date -d in the same way. On macOS, date arithmetic usually uses options such as -v. If you are writing scripts for both Linux and macOS, test the date command on each target system.

Bash date format mistakes to check in scripts

Date format specifiers are case-sensitive. A small change in letter case can produce a completely different value.

  • Use %m for month number, not %M. The %M option prints minutes.
  • Use %Y for a four-digit year and %y for a two-digit year.
  • Use quotes around the format string when it contains spaces, commas, colons mixed with text, or shell-sensitive characters.
  • Use %H for 24-hour time and %I with %p when you need a 12-hour clock with AM/PM.
  • Prefer %F or %Y-%m-%d for script-friendly dates that sort correctly.

Frequently asked questions on Bash date format options

How do I print date in YYYY-MM-DD format in Bash?

Use date +%F or date +%Y-%m-%d. Both print the date in YYYY-MM-DD format on systems that support the common GNU date format specifiers.

What is the difference between %m and %M in Bash date?

%m prints the month number from 01 to 12. %M prints minutes from 00 to 59. This is one of the most common mistakes in Bash date formatting.

How do I include spaces in a Bash date format?

Put the format string in quotes, for example date '+%A %d %B %Y'. The plus sign must come before the format string, and each date part should use its own percent specifier.

How can I create a timestamp for a filename in Bash?

Use a compact format such as date +%Y%m%d_%H%M%S. For example, backup_$(date +%Y%m%d_%H%M%S).tar.gz creates a filename with year, month, day, hour, minute, and second.

Does the Bash date command work the same on Linux and macOS?

Basic formatting options such as %Y, %m, %d, %H, %M, and %S are widely used. Date arithmetic options differ: GNU/Linux commonly supports date -d, while macOS/BSD date uses different options for relative dates.

Editorial QA checklist for Bash date format examples

  • Check that every Bash date example uses the correct case for format specifiers such as %m and %M.
  • Verify that examples with spaces use quoted format strings after the plus sign.
  • Confirm that output examples match the command format, especially for MM-YYYY and YYYY-MM-DD patterns.
  • Mention Linux/macOS differences when using date arithmetic such as date -d.
  • Prefer script-safe timestamp formats for filenames, backups, and logs.

Conclusion

Concluding this Bash TutorialBash Date, we have learned to use date command, the list of Bash Date Format options available with the date command and some of the examples demonstrating the usage of the format options. For most scripts, remember the essentials: %Y for four-digit year, %m for month, %d for day, %H:%M:%S for 24-hour time, and %F for the commonly used YYYY-MM-DD date format.