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
$ 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
$ 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
$ 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.
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 option | Part of Date | Description | Example Output |
|---|---|---|---|
| date +%a | Weekday | Name of weekday in short (like Sun, Mon, Tue, Wed, Thu, Fri, Sat) | Mon |
| date +%A | Weekday | Name of weekday in full (like Sunday, Monday, Tuesday) | Monday |
| date +%b | Month | Name of Month in short (like Jan, Feb, Mar ) | Jan |
| date +%B | Month | Month name in full (like January, February) | January |
| date +%d | Day | Day of month (e.g., 01) | 04 |
| date +%D | MM/DD/YY | Current Date; shown in MM/DD/YY | 02/18/18 |
| date +%F | YYYY-MM-DD | Date; shown in YYYY-MM-DD | 2018-01-19 |
| date +%H | Hour | Hour in 24-hour clock format | 18 |
| date +%I | Hour | Hour in 12-hour clock format | 10 |
| date +%j | Day | Day of year (001..366) | 152 |
| date +%m | Month | Number of month (01..12) (01 is January) | 05 |
| date +%M | Minutes | Minutes (00..59) | 52 |
| date +%S | Seconds | Seconds (00..59) | 18 |
| date +%N | Nanoseconds | Nanoseconds (000000000..999999999) | 300231695 |
| date +%T | HH:MM:SS | Time as HH:MM:SS (Hours in 24 Format) | 18:55:42 |
| date +%u | Day of Week | Day of week (1..7); 1 is Monday | 7 |
| date +%U | Week | Displays week number of year, with Sunday as first day of week (00..53) | 23 |
| date +%Y | Year | Displays full year i.e. YYYY | 2018 |
| date +%Z | Timezone | Time 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 option | Meaning | Example output | Common use |
|---|---|---|---|
date +%y | Year without century | 17 | Short dates |
date +%e | Day of month padded with a space | 4 | Human-readable reports |
date +%R | Hour and minute in 24-hour time | 18:55 | Short time display |
date +%r | 12-hour clock time with AM/PM | 06:55:42 PM | Human-readable time |
date +%s | Seconds since Unix epoch | 1514615139 | Time comparison in scripts |
date +%z | Numeric time zone offset | +0530 | Log timestamps |
date +%V | ISO week number | 52 | Week-based reports |
date +%c | Locale date and time | Sat 30 Dec 2017 11:55:39 AM IST | Readable 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
#!/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.
today=$(date +%F)
echo "$today"
2017-12-30
You can also write the same format explicitly.
date +%Y-%m-%d
Bash date format MM-YYYY
To format date in MM-YYYY format, use the command date +%m-%Y .
Bash Script
#!/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
#!/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.
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.
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.
date -d '2017-12-30' '+%A, %d %B %Y'
Saturday, 30 December 2017
Relative dates can also be used on GNU/Linux systems.
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
%mfor month number, not%M. The%Moption prints minutes. - Use
%Yfor a four-digit year and%yfor a two-digit year. - Use quotes around the format string when it contains spaces, commas, colons mixed with text, or shell-sensitive characters.
- Use
%Hfor 24-hour time and%Iwith%pwhen you need a 12-hour clock with AM/PM. - Prefer
%For%Y-%m-%dfor 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
%mand%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 Tutorial – Bash 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.
TutorialKart.com