Skip to main content

How to Get Day of Week

• 3 min read
bash date day of week time scheduling date formatting

Quick Answer: Get Day of Week in Bash

To get the day of week, use date +%A for the full name (Monday-Sunday) or date +%a for abbreviated (Mon-Sun). For a specific date, use date -d "2026-02-21" +%A. The date command is the standard, most reliable method.

Quick Comparison: Day of Week Methods

MethodFormatBest ForPortability
date +%AFull nameDisplay, conditionalsAll systems
date +%aAbbreviatedCompact displayAll systems
date +%wNumber (0-6)CalculationsAll systems
Array lookupCustomFlexible displayBash only
CalculationNumericComplex logicBash only

Bottom line: Use date +%A for names, date +%w for numbers. Always use -d to specify dates.


Determine the day of week from a date string. Learn using date command, arrays, and calculations.

Method 1: Get Day of Week from Today (Simplest)

The date command with format specifiers is the standard, most portable approach:

# Get today's day of week
date +%A

# Output: Friday

# Get abbreviated day
date +%a

# Output: Fri

# Get day number (0=Sunday, 6=Saturday)
date +%w

# Output: 5

When to Use Basic date Command

Use this when:

  • You need today’s day of week
  • You want the simplest, most readable code
  • Portability across systems is important
  • You’re just displaying the day name

Method 2: Get Day of Week from Specific Date

# Convert date string to day of week
date -d "2026-02-21" +%A

# Output: Saturday

# Get abbreviated day
date -d "2026-02-21" +%a

# Output: Sat

When to Use Specific Date Method

Use this when:

  • You have a date string that needs conversion
  • You’re processing date data from files or user input
  • You need to determine the day for past or future dates

Method 3: Day of Week from Epoch Timestamp

# Convert epoch to day of week
date -d @1645382400 +%A

# Output: Saturday

Function for Day of Week

#!/bin/bash

get_day_of_week() {
  local date="$1"

  if [ -z "$date" ]; then
    # Use today if no date provided
    date +%A
  else
    # Convert provided date
    date -d "$date" +%A
  fi
}

# Usage
get_day_of_week             # Today's day
get_day_of_week "2026-02-21"  # Specific date

# Output:
# Friday
# Saturday

Day of Week Number

#!/bin/bash

get_day_number() {
  local date="$1"

  # %w: 0=Sunday, 6=Saturday
  if [ -z "$date" ]; then
    date +%w
  else
    date -d "$date" +%w
  fi
}

# Usage
get_day_number "2026-02-21"

# Output: 6 (Saturday)

Detailed Example: All Format Options

#!/bin/bash

date_str="2026-02-21"

echo "Date: $date_str"
echo "Full day name: $(date -d "$date_str" +%A)"
echo "Short day name: $(date -d "$date_str" +%a)"
echo "Day number (0-6): $(date -d "$date_str" +%w)"
echo "Day of year (1-365): $(date -d "$date_str" +%j)"
echo "Week number (0-53): $(date -d "$date_str" +%W)"
echo "ISO week (1-53): $(date -d "$date_str" +%V)"

Output:

Date: 2026-02-21
Full day name: Saturday
Short day name: Sat
Day number (0-6): 6
Day of year (1-365): 052
Week number (0-53): 07
ISO week (1-53): 07

Check if Weekday

#!/bin/bash

is_weekday() {
  local date="$1"

  day_num=$(date -d "$date" +%w)

  # 0 and 6 are weekend (Sunday and Saturday)
  if [ "$day_num" -eq 0 ] || [ "$day_num" -eq 6 ]; then
    return 1  # False (weekend)
  else
    return 0  # True (weekday)
  fi
}

# Usage
if is_weekday "2026-02-21"; then
  echo "Weekday"
else
  echo "Weekend"
fi

# Output: Weekend (Saturday)

Check if Specific Day

#!/bin/bash

is_friday() {
  local date="$1"

  day=$(date -d "$date" +%A)

  if [ "$day" = "Friday" ]; then
    return 0
  else
    return 1
  fi
}

# Usage
if is_friday "2026-02-20"; then
  echo "It's Friday!"
else
  echo "Not Friday"
fi

Practical Example: Schedule Messages

#!/bin/bash

# File: daily_message.sh

today=$(date +%A)

case "$today" in
  Monday)
    echo "Start of week - Time to plan!"
    ;;
  Friday)
    echo "Happy Friday! Weekend is near!"
    ;;
  Saturday|Sunday)
    echo "Enjoy your weekend!"
    ;;
  *)
    echo "Just another day"
    ;;
esac

Output:

Happy Friday! Weekend is near!

Day of Week Array Reference

#!/bin/bash

# Create array of day names
days=("Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday")

# Get day number and lookup in array
date_str="2026-02-21"
day_num=$(date -d "$date_str" +%w)
day_name=${days[$day_num]}

echo "Date: $date_str"
echo "Day: $day_name"

Output:

Date: 2026-02-21
Day: Saturday

Practical Example: Weekly Report Generator

#!/bin/bash

# File: weekly_report.sh

today=$(date +%A)
date_str=$(date +%Y-%m-%d)

report_file="weekly_report_$date_str.txt"

{
  echo "=== Weekly Report ==="
  echo "Generated: $(date)"
  echo "Today: $today"
  echo ""

  if [ "$today" = "Friday" ]; then
    echo "End of week report"
    echo "Summary of the week's activities"
  elif [ "$today" = "Monday" ]; then
    echo "Start of week report"
    echo "Planning for the upcoming week"
  else
    echo "Mid-week progress report"
  fi
} > "$report_file"

echo "Report saved: $report_file"

Find Next Occurrence of Day

#!/bin/bash

find_next_day() {
  local target_day="$1"  # e.g., "Friday"
  local days=("Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday")

  # Get today's day number
  today=$(date +%w)

  # Find target day number
  target_num=-1
  for i in "${!days[@]}"; do
    if [ "${days[$i]}" = "$target_day" ]; then
      target_num=$i
      break
    fi
  done

  if [ "$target_num" -eq -1 ]; then
    echo "ERROR: Invalid day name"
    return 1
  fi

  # Calculate days until target
  if [ "$target_num" -gt "$today" ]; then
    days_until=$((target_num - today))
  else
    days_until=$((7 - today + target_num))
  fi

  # Calculate and display next occurrence
  next_date=$(date -d "+$days_until days" +%Y-%m-%d)
  echo "Next $target_day: $next_date"
}

# Usage
find_next_day "Friday"

Output:

Next Friday: 2026-02-27

Day of Week for Date Range

#!/bin/bash

# Show all days in date range

start_date="2026-02-21"
end_date="2026-02-28"

current=$(date -d "$start_date" +%s)
end=$(date -d "$end_date" +%s)

while [ "$current" -le "$end" ]; do
  date_str=$(date -d @$current +%Y-%m-%d)
  day=$(date -d @$current +%A)
  echo "$date_str : $day"

  current=$((current + 86400))  # Add 1 day in seconds
done

Output:

2026-02-21 : Saturday
2026-02-22 : Sunday
2026-02-23 : Monday
...
2026-02-28 : Saturday

Common Mistakes

  1. Using wrong date format - use YYYY-MM-DD for consistency
  2. Off-by-one in calculations - remember %w is 0-6
  3. Forgetting -d flag - needed to specify date other than today
  4. Not handling date parsing errors - check if date is valid
  5. Timezone issues - date uses local timezone

Performance Tips

  • Cache day calculations in variables
  • Use %w (number) instead of %A if just checking ranges
  • Avoid repeated date calls in loops

Key Points

  • Use date +%A for full day name
  • Use date +%w for day number (0-6)
  • Use date -d "YYYY-MM-DD" for specific dates
  • Remember: 0=Sunday, 6=Saturday
  • Handle weekday checks with conditional logic

Summary

Getting the day of week is straightforward with the date command. Use %A for full names, %a for abbreviations, and %w for numbers. Combining with conditionals lets you build date-aware scheduling and reporting scripts.