YYYY-MM-DD format date in shell script
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
To get the current date in YYYY-MM-DD format in a shell script, use date +%Y-%m-%d. This works on Linux (GNU coreutils) and macOS (BSD) and produces output like 2026-06-18. The format string %Y gives the four-digit year, %m the zero-padded month, and %d the zero-padded day.
Basic Usage
Assign the formatted date to a variable and use it in your script:
Output:
The $(...) syntax runs the date command in a subshell and captures its output. No quotes are needed around the format string as long as it contains no spaces, but quoting is a good defensive habit:
Common Date Format Variations
The date command supports many format specifiers. Here are the most useful combinations:
| Format String | Example Output | Description |
+%Y-%m-%d | 2026-06-18 | ISO 8601 date |
+%Y%m%d | 20260618 | Compact date (good for filenames) |
+%Y-%m-%d_%H-%M-%S | 2026-06-18_14-30-00 | Date and time (filename-safe) |
+%Y-%m-%dT%H:%M:%S%z | 2026-06-18T14:30:00+0000 | ISO 8601 with timezone |
+%s | 1781856600 | Unix timestamp (seconds since epoch) |
+%A, %B %d, %Y | Thursday, June 18, 2026 | Human-readable long format |
+%d/%m/%Y | 18/06/2026 | European date format |
Date Arithmetic
GNU date (Linux)
GNU date supports the -d (or --date) option for date arithmetic:
BSD date (macOS)
macOS uses BSD date, which has a different syntax for date arithmetic:
Cross-Platform Date Arithmetic
If your script needs to run on both Linux and macOS, detect the platform:
Practical Script Examples
Log Rotation with Date Stamps
Timestamped Backup Script
Date Range Loop
Iterate over a range of dates (GNU date):
Comparing Dates
Compare dates as strings since YYYY-MM-DD format sorts lexicographically:
This works because the YYYY-MM-DD format preserves chronological order in string comparison. Formats like MM/DD/YYYY do not have this property.
UTC vs. Local Time
By default, date uses the system's local timezone. For consistent results across servers in different timezones, use UTC:
Using UTC is especially important in cron jobs and distributed systems where servers may be in different timezones.
Format Specifier Reference
| Specifier | Meaning | Example |
%Y | Four-digit year | 2026 |
%y | Two-digit year | 26 |
%m | Month (01-12) | 06 |
%d | Day of month (01-31) | 18 |
%H | Hour, 24-hour (00-23) | 14 |
%M | Minute (00-59) | 30 |
%S | Second (00-59) | 45 |
%s | Unix timestamp | 1781856600 |
%Z | Timezone abbreviation | UTC |
%z | Timezone offset | +0000 |
%A | Full weekday name | Thursday |
%a | Abbreviated weekday | Thu |
%B | Full month name | June |
%b | Abbreviated month | Jun |
%j | Day of year (001-366) | 169 |
%u | Day of week (1=Mon, 7=Sun) | 4 |
Common Pitfalls
Confusing %m with %M. %m is the month, %M is the minute. Writing date +%Y-%M-%d gives you the minute in the month position, producing nonsensical dates like 2026-30-18.
Using uppercase %D. The %D specifier produces MM/DD/YY (American short format), not YYYY-MM-DD. This is a common source of confusion.
Forgetting the + before the format string. Writing date %Y-%m-%d without the + causes date to try to parse it as a date string, not as a format specifier.
Assuming GNU date on macOS. macOS ships with BSD date, which does not support -d. Use -v for date arithmetic on macOS, or install GNU coreutils via Homebrew (gdate).
Using backticks instead of $(...). While backticks work, they do not nest cleanly and are harder to read. The $(...) syntax is the modern standard:
Not quoting variables in comparisons. If the date variable is empty for any reason, an unquoted comparison like [ $today = $deadline ] will produce a syntax error. Always use double brackets or quote the variables: [[ "$today" == "$deadline" ]].
Summary
Use date +%Y-%m-%d to get the current date in ISO 8601 format in any shell script. For date arithmetic, use -d on Linux (GNU) or -v on macOS (BSD). The YYYY-MM-DD format is ideal for filenames, logs, and date comparisons because it sorts chronologically as a plain string. Use UTC (date -u) for consistent results across timezones. Watch out for the %m vs. %M confusion, and remember that macOS and Linux have different date command syntax for offsets.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.