Mastering linux date util

Date :

Part of gnu/linux coreutils


I’ve some tricks and basic knowledge around date cli I want to share it with our members here at forums, it might be useful for user whose spend 80% of time in terminal,
Keep in mind that date util has calendar awareness it knows leap year
and aware of months with 28/31 days etc.

1

change the system clock

date +%T -s "10:13:13" # set time 24H
date -s "11:48:00 PM" # set time 12H
sudo date -s "2026-02-01 00:00:00" # set date and time
# use hwclock to write/read -> cmos

2

count duration between two dates

echo $(( ($(date -d 2026-1-1 +%s) - $(date -d 2025-1-1 +%s)) / 86400 )) # Days
echo $(( ($(date -d 2026-1-1 +%s) - $(date -d 2025-1-1 +%s)) / 604800 )) # Weeks

Chronosphere ||| count everything in between :stopwatch:

d1="2025-01-01" d2="2026-01-01"; s=$(( $(date -d "$d2" +%s) - $(date -d "$d1" +%s) )); echo "Days: $(( s / 86400 ))  Weeks: $(( s / 604800 ))  Months: $(( ( $(date -d "$d2" +%Y) - $(date -d "$d1" +%Y) ) * 12 + $(date -d "$d2" +%m) - $(date -d "$d1" +%m) ))  Years: $(( $(date -d "$d2" +%Y) - $(date -d "$d1" +%Y) ))"

3

Print day name from date

date -d "2026-02-28" +"%A" 

4

print the date from 3 days ago

date --date='3 days ago' 

print the date after 42 days

date --date='42 days'

print date after 5 months and 2 days

date --date='5 months 2 day'

Unix Timestamp

date +%s





Thanks for reading , &I hope you learn something new :sparkles:

5 Likes

Nice one! Reminding us how versatile the GNU coreutils are. Thanks for putting this together.

2 Likes

date “+%r, %D” is another example; it produces output as shown below:
09:29:06 PM, 11/05/25

3 Likes

Thanks @Halano for this explanation :clap:

2 Likes