Many developers working in international teams or handling cross-timezone projects require consistent date formatting. The ISO 8601 standard (YYYY-MM-DD HH:MM:SS) provides this consistency while being:
- Sortable by alphabetical order
- Unambiguous across regions
- Compatible with most databases
- Human-readable
The most reliable method to enforce ISO format globally is through locale settings. Edit the locale configuration file:
sudo nano /etc/default/locale
Add or modify these variables:
LC_TIME=en_DK.UTF-8
LC_ALL=en_US.UTF-8
LANG=en_US.UTF-8
The en_DK
locale specifically uses ISO date formats while maintaining English language output.
For temporary changes or containerized environments, set these variables in your .bashrc
or shell profile:
export LC_TIME=en_DK.UTF-8
export TIME_STYLE=+"%Y-%m-%d %H:%M:%S"
Verify the changes with these commands:
date
# Should output: 2023-11-15 14:30:22
ls -l --time-style=+"%Y-%m-%d %H:%M:%S"
# Shows files with ISO timestamps
Some applications may need additional configuration:
For PostgreSQL:
ALTER DATABASE your_db SET datestyle TO 'ISO, YMD';
For Python scripts:
import locale
locale.setlocale(locale.LC_TIME, 'en_DK.UTF-8')
On older systems without en_DK
locale, create a custom locale:
sudo localedef -i en_US -f UTF-8 en_US.UTF-8_ISO
Then modify the date format in /usr/share/i18n/locales/en_US
before compiling.
- If changes don't apply:
sudo locale-gen
and reboot - Missing locales:
sudo apt-get install locales-all
- Docker containers: Set ENV variables in Dockerfile
Many Linux applications inherit their datetime formatting from system locale settings rather than implementing custom formats. The ISO 8601 standard (YYYY-MM-DD HH:MM:SS) offers several advantages:
- Lexicographical order matches chronological order
- Unambiguous across international regions
- Widely supported in programming languages
1. Modifying locale configuration files
Edit /etc/locale.conf
(systemd systems) or /etc/default/locale
(Debian/Ubuntu):
LC_TIME="en_US.UTF-8"
TIME_STYLE="+%Y-%m-%d %H:%M:%S"
2. Environment Variable Overrides
Add these to /etc/environment
or shell rc files:
export LC_TIME=en_DK.UTF-8
export TIME_STYLE=iso
For GNU coreutils (ls, date commands)
alias date='date "+%Y-%m-%d %H:%M:%S"'
alias ls='ls --time-style="+%Y-%m-%d %H:%M:%S"'
PHP Configuration
date_default_timezone_set('UTC');
ini_set('date.format', 'Y-m-d H:i:s');
Python Standardization
import datetime
datetime.datetime.now().isoformat(sep=' ', timespec='seconds')
After making changes, verify with:
date
ls -l --time-style=+"%Y-%m-%d %H:%M:%S"
For systemd services, check with:
systemctl show-environment | grep LC_TIME
Older systems may require additional steps:
sudo locale-gen en_DK.UTF-8
sudo update-locale LC_TIME=en_DK.UTF-8
The DK locale uses ISO formatting by default, making it a good fallback when custom formats aren't respected.