How to Get Current Date in Bash
Quick Answer: Get Current Date in Bash
Use the date command to display the current date. By default it shows the full date and time, but you can format it with + to customize the output: date +%Y-%m-%d for YYYY-MM-DD format.
Quick Comparison: Date Display Methods
| Method | Output | Best For | Notes |
|---|---|---|---|
| date (plain) | Full date and time | Quick viewing | Shows timezone and day name |
| date +format | Customized format | Scripts and logs | Most flexible approach |
| date +%s | Unix timestamp | Calculations | Seconds since epoch |
| date -u | UTC time | Server logging | Timezone-independent |
Bottom line: Use date +%Y-%m-%d for standard logging, date +%s for comparisons, and date without arguments for quick viewing.
Method 1: Basic Date Display
The simplest approach is running date with no options. This immediately shows your system’s current date and time with the default format:
date
# Output: Sat Feb 21 12:30:45 UTC 2026
When to Use Basic Date Display
- You just need to see the current date quickly
- You’re working interactively in the terminal
- Timezone information matters for your context
- You want the full day name and month name included
Method 2: Formatted Date Output
Format the date output using + followed by format codes. This gives you complete control over what gets displayed and in what order. The + character tells date you want custom formatting:
date +%Y-%m-%d
# Output: 2026-02-21
date +%d/%m/%Y
# Output: 21/02/2026
date +"%A, %B %d, %Y"
# Output: Saturday, February 21, 2026
When to Use Formatted Output
- You’re creating log files or backup names
- You need a specific format for your application
- You’re writing data to a configuration file
- The output needs to be parseable by other tools
- You want consistent formatting across different locales
Date Format Codes
| Code | Meaning |
|---|---|
%Y | Year (4 digits) |
%m | Month (01-12) |
%d | Day (01-31) |
%H | Hour (00-23) |
%M | Minute (00-59) |
%S | Second (00-59) |
%A | Full day name |
%B | Full month name |
%j | Day of year |
%w | Day of week (0-6) |
Method 3: Unix Timestamp
Get the number of seconds since January 1, 1970. This is essential for comparing dates, calculating time differences, or storing dates in a compact form:
date +%s
# Output: 1740139845
When to Use Unix Timestamp
- You need to compare two dates mathematically
- You’re storing dates in a database
- You want timezone-independent time measurement
- You need to calculate elapsed time between events
Method 4: Storing Date in Variables
Capture the date output into a variable for use in scripts and automation tasks:
today=$(date +%Y-%m-%d)
echo "Today is: $today"
timestamp=$(date +%s)
echo "Unix time: $timestamp"
When to Use Variable Storage
- You need the date value multiple times in a script
- You’re constructing filenames or paths dynamically
- You want to log the date along with other data
- You’re creating timestamped backups or exports
Common Date Formats
# ISO format (YYYY-MM-DD)
date +%Y-%m-%d
# Long format
date +"%A, %B %d, %Y"
# Time with seconds
date +%H:%M:%S
# Timestamp format
date +"%Y%m%d_%H%M%S" # 20260221_123045
# Unix timestamp
date +%s # Seconds since epoch
Practical Examples
Filename with Date
Create backup files with automatic date naming:
#!/bin/bash
backup_file="backup_$(date +%Y%m%d_%H%M%S).tar.gz"
tar -czf "$backup_file" /home/user/data
echo "Backup saved: $backup_file"
Get Previous/Next Day
Calculate relative dates using GNU date or macOS equivalents:
# Previous day (GNU date)
date -d "1 day ago" +%Y-%m-%d
# Next day
date -d "1 day" +%Y-%m-%d
# macOS date
date -v-1d +%Y-%m-%d # Previous day
Timezone Handling
Work with different timezones when needed:
# Current timezone
date +%Z
# UTC time
date -u +%Y-%m-%d
# Specific timezone (GNU)
TZ=America/New_York date
Quick Reference
# Basic usage
date # Full date and time
# Common formats
date +%Y-%m-%d # YYYY-MM-DD
date +%Y/%m/%d # YYYY/MM/DD
date +%H:%M:%S # HH:MM:SS
date +%Y%m%d_%H%M%S # YYYYMMDD_HHMMSS
# Unix timestamp
date +%s # Seconds since epoch
# UTC time
date -u +%Y-%m-%d # UTC date
# With timezone
TZ=UTC date +%Y-%m-%d # Specific timezone
Summary
The date command is your go-to tool for handling dates in Bash. Use date by itself for quick viewing, date +format for custom output in scripts, and date +%s for timezone-independent calculations. It’s essential for logging, creating timestamped backups, and automating any task that depends on the current date or time.