I have been experimenting with calculating the number of days between two dates for a script that I am writing.
This works fine:
keith@E5570:~$ echo $(( ($(date +%s) - $(date -d 5/31/2025 +%s))/86400 ))
8
keith@E5570:~$
where the “%s” means report the number of seconds since the reference date of 1/1/2007 and 86400 is the number seconds in one day.
A minor irritation is that I have to specify the subtractant date in USA format, i.e. mm/dd/yyyy rather than my UK locale format of dd/mm/yyyy.
Curiously my locale is set correctly:
keith@E5570:~$ locale
LANG=en_GB.UTF-8
LANGUAGE=
LC_CTYPE=“en_GB.UTF-8”
LC_NUMERIC=“en_GB.UTF-8”
LC_TIME=“en_GB.UTF-8”
etc etc…
Digging further, we get get detailed info on the en_GB locale at /usr/share/i18n/locales/en_GB where we find under the LC_TIME paragraph:
d_t_fmt “%a %d %b %Y %T %Z” (e.g. Sat 24 Jun 2025 15:02:30 GMT)**
d_fmt “%d//%m//%y” (e.g. 24/06/2025)
t_fmt “%T”
am_pm “am”;“pm”
t_fmt_ampm “%l:%M:%S %P %Z”
date_fmt “%a %e %b %H:%M:%S %Z %Y”
(e.g. simple date command → Sat 24 Jun 15:02:30 GMT 2025 correctly)
week 7;19971130;4
first_weekday 2
Which looks fine except that when I specify a date (e.g. date -d …) it only accepts mm/dd/yyyy format:
keith@E5570:~$ date -d 5/31/2005
Tue 31 May 00:00:00 BST 2005
keith@E5570:~$ date -d 31/5/2005
date: invalid date ‘31/5/2005’
keith@E5570:~$
Even trying to specify the format fails:
keith@E5570:~$ date -d 31/5/2005 +%d/%m/%Y
date: invalid date ‘31/5/2005’
keith@E5570:~$
but
keith@E5570:~$ date -d 5/31/2005 +%d/%m/%Y
31/05/2005
keith@E5570:~$
formats correctly. This doesn’t matter, now I know about it, but it’s irritating.
Any ideas, anyone?
1 post - 1 participant


