Skip to content

Latest commit

 

History

History
718 lines (602 loc) · 15.5 KB

date-format-codes.md

File metadata and controls

718 lines (602 loc) · 15.5 KB
title DATE FORMAT CODES - COMPLETE REFERENCE
description Complete guide to DATE FORMAT CODES - COMPLETE REFERENCE
category General
tags
linux
difficulty beginner
last_updated 2026-06-06
related_commands

DATE FORMAT CODES - COMPLETE REFERENCE

Overview

Command: date (GNU coreutils) Purpose: Format date and time output Syntax: date +FORMAT Total Format Codes: 42 standard codes Platform: Linux, macOS, BSD, Unix-like systems

Purpose

Date format codes are used to customize the output of the date command. Each code is prefixed with % and represents a specific component of date/time information.

Complete Format Code Table

Code Category Description Example Output Range/Format
%% Literal Literal % character % Single %
%a Weekday Locale's abbreviated weekday name Sun, Mon, Tue 3-letter abbreviation
%A Weekday Locale's full weekday name Sunday, Monday, Tuesday Full name
%b Month Locale's abbreviated month name Jan, Feb, Mar 3-letter abbreviation
%B Month Locale's full month name January, February, March Full name
%c Combined Locale's date and time Thu Jan 1 08:30:00 2009 Locale-dependent
%C Year Century (year divided by 100) 20, 21 2 digits
%d Day Day of month (zero-padded) 01, 15, 31 01-31
%D Combined Date in mm/dd/yy format 01/01/09, 12/31/24 US short date
%e Day Day of month (space-padded) 1, 15, 31 1-31 (space padded)
%F Combined Full date YYYY-MM-DD (ISO 8601) 2009-01-01, 2024-12-31 ISO 8601 date
%g Year Last 2 digits of ISO week-based year 09, 24 00-99
%G Year ISO week-based year (4 digits) 2009, 2024 Full year
%h Month Abbreviated month (same as %b) Jan, Feb, Mar Same as %b
%H Hour Hour in 24-hour format (00-23) 00, 08, 23 00-23
%I Hour Hour in 12-hour format (01-12) 01, 08, 12 01-12
%j Day Day of year (001-366) 001, 180, 365 001-366
%k Hour Hour in 24-hour (space-padded 0-23) 0, 8, 23 0-23 (space padded)
%l Hour Hour in 12-hour (space-padded 1-12) 1, 8, 12 1-12 (space padded)
%m Month Month number (01-12) 01, 06, 12 01-12
%M Minute Minute (00-59) 00, 30, 59 00-59
%n Literal Newline character (newline) Line break
%N Nanosecond Nanoseconds (000000000-999999999) 123456789, 000000000 000000000-999999999
%p Period AM or PM (uppercase) AM, PM Uppercase
%P Period am or pm (lowercase) am, pm Lowercase
%r Combined 12-hour time with AM/PM 08:30:00 AM, 11:45:30 PM Locale's 12-hour time
%R Combined 24-hour time HH:MM 08:30, 23:45 HH:MM format
%s Timestamp Seconds since Unix epoch 1230825000, 1704153600 Unix timestamp
%S Second Seconds (00-60) 00, 30, 60 00-60 (60 for leap)
%t Literal Tab character (tab) Horizontal tab
%T Combined 24-hour time HH:MM:SS 08:30:00, 23:45:30 HH:MM:SS format
%u Weekday Day of week (1-7, Monday=1) 1, 4, 7 1=Mon, 7=Sun
%U Week Week number, Sunday as first day 00, 26, 53 00-53
%V Week ISO week number (01-53) 01, 26, 53 01-53
%w Weekday Day of week (0-6, Sunday=0) 0, 3, 6 0=Sun, 6=Sat
%W Week Week number, Monday as first day 00, 26, 53 00-53
%x Combined Locale's date representation 01/01/09, 12/31/24 Locale-dependent
%X Combined Locale's time representation 08:30:00, 23:45:30 Locale-dependent
%y Year Last 2 digits of year 09, 24, 99 00-99
%Y Year Full year (4 digits) 2009, 2024, 2099 Full year
%z Timezone Timezone offset from UTC -0800, +0530, +0000 ±HHMM
%Z Timezone Timezone name or abbreviation PST, EST, UTC, IST Timezone name
%:z Timezone Timezone offset with colon -08:00, +05:30, +00:00 ±HH:MM
%::z Timezone Timezone offset with seconds -08:00:00, +05:30:00 ±HH:MM:SS
%:::z Timezone Compact timezone offset -08, +05, +00 ±HH (minimal)

Detailed Code Explanations

Literal Characters

%% - Literal Percent

date '+%%'
# Output: %

date '+Progress: 50%%'
# Output: Progress: 50%

%n - Newline

date '+Date: %F%nTime: %T'
# Output:
# Date: 2024-01-15
# Time: 14:30:00

%t - Tab

date '+%Y%t%m%t%d'
# Output: 2024    01      15

Weekday Codes

%a - Abbreviated Weekday

date '+%a'
# Monday    → Mon
# Tuesday   → Tue
# Wednesday → Wed
# Thursday  → Thu
# Friday    → Fri
# Saturday  → Sat
# Sunday    → Sun

%A - Full Weekday

date '+%A'
# Output: Monday, Tuesday, Wednesday, etc.

%u - Weekday Number (ISO 8601)

date '+%u'
# Monday    = 1
# Tuesday   = 2
# Wednesday = 3
# Thursday  = 4
# Friday    = 5
# Saturday  = 6
# Sunday    = 7

%w - Weekday Number (US Convention)

date '+%w'
# Sunday    = 0
# Monday    = 1
# Tuesday   = 2
# Wednesday = 3
# Thursday  = 4
# Friday    = 5
# Saturday  = 6

Month Codes

%b / %h - Abbreviated Month

date '+%b'
# January   → Jan
# February  → Feb
# March     → Mar
# April     → Apr
# May       → May
# June      → Jun
# July      → Jul
# August    → Aug
# September → Sep
# October   → Oct
# November  → Nov
# December  → Dec

%B - Full Month Name

date '+%B'
# Output: January, February, March, etc.

%m - Month Number

date '+%m'
# January   = 01
# February  = 02
# March     = 03
# April     = 04
# May       = 05
# June      = 06
# July      = 07
# August    = 08
# September = 09
# October   = 10
# November  = 11
# December  = 12

Day Codes

%d - Day of Month (Zero-Padded)

date '+%d'
# 1st  → 01
# 10th → 10
# 31st → 31

%e - Day of Month (Space-Padded)

date '+%e'
# 1st  →  1  (with leading space)
# 10th → 10
# 31st → 31

%j - Day of Year

date '+%j'
# Jan 1   → 001
# Jun 1   → 152 (non-leap) / 153 (leap)
# Dec 31  → 365 (non-leap) / 366 (leap)

# Example: Find which day number today is
date '+Today is day %j of %Y'
# Output: Today is day 015 of 2024

Year Codes

%Y - Full Year (4 digits)

date '+%Y'
# Output: 2024, 2009, 1999

%y - Last 2 Digits of Year

date '+%y'
# 2024 → 24
# 2009 → 09
# 1999 → 99
# 2000 → 00

%C - Century

date '+%C'
# 1999 → 19
# 2000 → 20
# 2024 → 20
# 2100 → 21

%G - ISO Week-Based Year

# Important: %G can differ from %Y at year boundaries
date -d "2024-01-01" '+%Y-%G'
# Output: 2024-2024

date -d "2023-01-01" '+%Y-%G'
# Output: 2023-2022 (because Jan 1, 2023 is in week 52 of 2022)

%g - Last 2 Digits of ISO Week-Based Year

date -d "2023-01-01" '+%y-%g'
# Output: 23-22

Hour Codes

%H - 24-Hour Format (00-23)

date '+%H'
# Midnight → 00
# 8 AM     → 08
# Noon     → 12
# 8 PM     → 20
# 11 PM    → 23

%I - 12-Hour Format (01-12)

date '+%I'
# Midnight → 12
# 8 AM     → 08
# Noon     → 12
# 8 PM     → 08
# 11 PM    → 11

%k - 24-Hour (Space-Padded)

date '+%k'
# Midnight →  0  (with leading space)
# 8 AM     →  8  (with leading space)
# 10 AM    → 10
# 11 PM    → 23

%l - 12-Hour (Space-Padded)

date '+%l'
# Midnight → 12
# 8 AM     →  8  (with leading space)
# Noon     → 12
# 8 PM     →  8  (with leading space)

Minute and Second Codes

%M - Minute (00-59)

date '+%M'
# Output: 00, 01, 30, 59

%S - Second (00-60)

date '+%S'
# Output: 00, 01, 30, 59, 60
# Note: 60 is for leap seconds

%N - Nanoseconds

date '+%N'
# Output: 123456789, 000000000, 999999999

# High-precision timestamp
date '+%s.%N'
# Output: 1704153600.123456789

AM/PM Codes

%p - Uppercase AM/PM

date '+%I:%M:%S %p'
# 8:30:00 AM
# 8:30:00 PM

%P - Lowercase am/pm

date '+%I:%M:%S %P'
# 8:30:00 am
# 8:30:00 pm

Week Number Codes

%U - Week Number (Sunday as First Day)

date '+%U'
# Week 1 starts on first Sunday
# Jan 1 (if not Sunday) is in week 00
# Range: 00-53

%W - Week Number (Monday as First Day)

date '+%W'
# Week 1 starts on first Monday
# Jan 1 (if not Monday) is in week 00
# Range: 00-53

%V - ISO Week Number

date '+%V'
# ISO 8601 week number
# Week 1 is first week with Thursday in it
# Range: 01-53

# Example: Show ISO week
date '+%G-W%V-%u'
# Output: 2024-W03-1 (Year 2024, Week 03, Monday)

Timezone Codes

%z - Timezone Offset (Compact)

date '+%z'
# UTC-8     → -0800
# UTC+5:30  → +0530
# UTC       → +0000

%:z - Timezone Offset (Colon)

date '+%:z'
# UTC-8     → -08:00
# UTC+5:30  → +05:30
# UTC       → +00:00

%::z - Timezone Offset (Seconds)

date '+%::z'
# UTC-8     → -08:00:00
# UTC+5:30  → +05:30:00
# UTC       → +00:00:00

%:::z - Timezone Offset (Minimal)

date '+%:::z'
# UTC-8     → -08
# UTC+5:30  → +05:30
# UTC       → +00

%Z - Timezone Name

date '+%Z'
# Output: PST, EST, UTC, IST, GMT, JST, etc.

Combined Format Codes

%c - Locale's Date and Time

date '+%c'
# Output: Thu Jan  1 08:30:00 2009
# (format varies by locale)

%D - Short Date (MM/DD/YY)

date '+%D'
# Output: 01/01/09, 12/31/24
# Equivalent to %m/%d/%y

%F - ISO 8601 Date (YYYY-MM-DD)

date '+%F'
# Output: 2024-01-15
# Equivalent to %Y-%m-%d
# **Most important for filenames and logs**

%R - Time Without Seconds (HH:MM)

date '+%R'
# Output: 08:30, 23:45
# Equivalent to %H:%M

%T - Full Time (HH:MM:SS)

date '+%T'
# Output: 08:30:00, 23:45:30
# Equivalent to %H:%M:%S

%r - 12-Hour Time with AM/PM

date '+%r'
# Output: 08:30:00 AM, 11:45:30 PM
# Equivalent to %I:%M:%S %p

%x - Locale's Date

date '+%x'
# US:   01/15/2024
# EU:   15/01/2024
# ISO:  2024-01-15
# (varies by locale)

%X - Locale's Time

date '+%X'
# Output: 08:30:00
# (varies by locale)

Unix Timestamp

%s - Seconds Since Epoch

date '+%s'
# Output: 1704153600

# Unix epoch: January 1, 1970 00:00:00 UTC
# Counts seconds since that moment

# Example: Calculate time difference
START=$(date '+%s')
# ... do something ...
END=$(date '+%s')
ELAPSED=$((END - START))
echo "Elapsed: $ELAPSED seconds"

Practical Examples

Log Timestamps

# Standard log format
date '+%Y-%m-%d %H:%M:%S'
# Output: 2024-01-15 14:30:00

# With timezone
date '+%Y-%m-%d %H:%M:%S %Z'
# Output: 2024-01-15 14:30:00 PST

# ISO 8601 with timezone
date '+%FT%T%:z'
# Output: 2024-01-15T14:30:00-08:00

Filename Timestamps

# Daily backup
backup-$(date '+%Y%m%d').tar.gz
# Result: backup-20240115.tar.gz

# With time
backup-$(date '+%Y%m%d-%H%M%S').tar.gz
# Result: backup-20240115-143000.tar.gz

# ISO format (sortable)
backup-$(date '+%FT%H%M%S').tar.gz
# Result: backup-2024-01-15T143000.tar.gz

Readable Dates

# Full format
date '+%A, %B %d, %Y'
# Output: Monday, January 15, 2024

# Short format
date '+%a, %b %d, %Y'
# Output: Mon, Jan 15, 2024

# With time
date '+%A, %B %d, %Y at %I:%M %p'
# Output: Monday, January 15, 2024 at 02:30 PM

ISO 8601 Formats

# Date only
date '+%F'
# Output: 2024-01-15

# Date and time
date '+%FT%T'
# Output: 2024-01-15T14:30:00

# With timezone
date '+%FT%T%:z'
# Output: 2024-01-15T14:30:00-08:00

# UTC (Zulu time)
date -u '+%FT%TZ'
# Output: 2024-01-15T22:30:00Z

RFC Formats

# RFC 2822 (email headers)
date -R
# Output: Mon, 15 Jan 2024 14:30:00 -0800

# RFC 3339
date --rfc-3339=seconds
# Output: 2024-01-15 14:30:00-08:00

Week Information

# ISO week date
date '+%G-W%V-%u'
# Output: 2024-W03-1

# Week number and day
date '+Week %V, %A'
# Output: Week 03, Monday

Relative Dates

# Yesterday
date -d "yesterday" '+%F'
# Output: 2024-01-14

# Next week
date -d "next week" '+%F'
# Output: 2024-01-22

# 30 days ago
date -d "30 days ago" '+%F'
# Output: 2023-12-16

Common Use Cases

Backup Scripts

#!/bin/bash
BACKUP_DIR="/backup"
TIMESTAMP=$(date '+%Y%m%d-%H%M%S')
tar czf "$BACKUP_DIR/backup-$TIMESTAMP.tar.gz" /data
echo "[$(date '+%F %T')] Backup completed: backup-$TIMESTAMP.tar.gz"

Log Files

#!/bin/bash
LOG_FILE="/var/log/app.log"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Application started" >> "$LOG_FILE"

Date Calculations

#!/bin/bash
# Days until end of year
TODAY=$(date '+%j')
YEAR=$(date '+%Y')
if [ $((YEAR % 4)) -eq 0 ]; then
    DAYS_IN_YEAR=366
else
    DAYS_IN_YEAR=365
fi
DAYS_LEFT=$((DAYS_IN_YEAR - TODAY))
echo "Days left in $YEAR: $DAYS_LEFT"

Monitoring Scripts

#!/bin/bash
# Check if backup is recent
BACKUP_FILE="/backup/latest.tar.gz"
BACKUP_TIME=$(date -r "$BACKUP_FILE" '+%s')
CURRENT_TIME=$(date '+%s')
AGE_HOURS=$(( (CURRENT_TIME - BACKUP_TIME) / 3600 ))

if [ $AGE_HOURS -gt 24 ]; then
    echo "[$(date '+%F %T')] ALERT: Backup is $AGE_HOURS hours old"
fi

Best Practices

DO

  • Use %F (YYYY-MM-DD) for filenames and logs (sortable)
  • Use %s for date calculations (Unix timestamp)
  • Use %T or %F %T for log timestamps
  • Use %FT%T%:z for ISO 8601 with timezone
  • Include timezone (%Z or %:z) in distributed systems
  • Quote format strings with single quotes to prevent shell expansion

DON'T

  • Don't use %D (MM/DD/YY) for filenames (ambiguous)
  • Don't use locale-dependent formats (%c, %x, %X) in scripts
  • Don't use spaces in filename timestamps (use hyphens or underscores)
  • Don't forget to escape % in printf-style strings
  • Don't use 2-digit years (%y) for long-term data

Locale Impact

Some format codes are locale-dependent:

# US English locale
export LC_TIME=en_US.UTF-8
date '+%A, %B %d, %Y'
# Output: Monday, January 15, 2024

# French locale
export LC_TIME=fr_FR.UTF-8
date '+%A, %B %d, %Y'
# Output: lundi, janvier 15, 2024

# German locale
export LC_TIME=de_DE.UTF-8
date '+%A, %B %d, %Y'
# Output: Montag, Januar 15, 2024

Locale-dependent codes: %a, %A, %b, %B, %c, %p, %P, %r, %x, %X

Quick Reference

# Common formats
date '+%F'              # 2024-01-15
date '+%T'              # 14:30:00
date '+%F %T'           # 2024-01-15 14:30:00
date '+%Y%m%d'          # 20240115
date '+%Y%m%d-%H%M%S'   # 20240115-143000
date '+%s'              # 1704153600 (Unix timestamp)
date '+%FT%T%:z'        # 2024-01-15T14:30:00-08:00 (ISO 8601)

# Readable formats
date '+%A, %B %d, %Y'   # Monday, January 15, 2024
date '+%b %d, %Y'       # Jan 15, 2024
date '+%I:%M %p'        # 02:30 PM

# Week formats
date '+%G-W%V-%u'       # 2024-W03-1 (ISO week)
date '+Week %V of %Y'   # Week 03 of 2024

# Timezone
date '+%Z'              # PST
date '+%:z'             # -08:00

References

  • GNU Coreutils Manual: man date
  • POSIX.1-2017 strftime specification
  • ISO 8601 Date and Time Format
  • RFC 2822 (email date format)
  • RFC 3339 (Internet date/time format)

Last Updated: 2024 Total Format Codes: 42 (including aliases and variants) Status: Complete date format code reference