When you type the date command the system shows the current date in the default format. In this guide, we learn how to change the default date format. Here we will be modifying the LC_TIME parameter within locale files.
Step 1: Identify Current Date Format
Before making any changes, it's essential to identify your current time format. To do so, you need to understand the following two terms:
- Locale: It is a collection of parameters that defines user's language, country, character encoding, the date and time format, currency format, etc.
- LC_TIME: It is one parameter of the locale that specifically controls how time and date are formatted and displayed.
Run the locale command and look for the LC_TIME variable.
For example, if you see LC_TIME="en_US.UTF-8", which is the commonly used date format in the United States.
Step 2: Locate File Associated with Locale
Locale files are typically located in /usr/share/i18n/locales/. You will find a file with the name that corresponds to your LC_TIME locale setting.
For example, if your LC_TIME locale is en_US.UTF-8, the file will be named en_US.
cd /usr/share/i18n/locales/ ls -l en_US
Step 3: Modify the Locale File
To modify the file, you'll need to use a text editor and have the necessary permissions (typically root permissions)
sudo vi en_US
Within the file, locate the LC_TIME section and make adjustments to the lines d_t_fmt, d_fmt and date_fmt. To facilitate your understanding of these variables, we have included a cheat sheet containing all the available format codes.
date_fmt is like the blueprint for displaying the current date and time when you run the "date" command without any extra instructions.
On the other hand, the d_fmt attribute controls how dates are displayed, excluding the time component. It is represented by the %x format code. Lastly, the d_t_fmt attribute combines both the date and time, specifying how they should be presented together in a single display. It is represented by the %c format code.
Let's look into a few examples of different locales formatting the date and time display.
Example 1 (en_US locale): This locale is set to display the full date and time with a 12-hour clock (
%r) including AM/PM designation, and the date format with slashes (
%m//%d//%Y), which is typical for the United States.:
d_t_fmt "%a %d %b %Y %r %Z" d_fmt "%m//%d//%Y" date_fmt "%a %b %e %r %Z %Y"
Output: Wed Nov 8 03:04:34 PM UTC 2023
Example 2 (Locale with
dd/mm/yyyy format). It uses a 24-hour clock (
%T) and places the day before the month in date representations.
d_t_fmt "%a %d %b %Y %T" d_fmt "%d//%m//%y" date_fmt "%a %d %b %Y %T %Z"
Output: Wed 08 Nov 2023 15:05:17 UTC
Example 3 (Custom locale with
Day dd/mm/yyyy): Here a custom locale, which uses a simplified output with the format “Day dd/mm/yyyy”
d_t_fmt "%A %d-%m-%Y" d_fmt "%d//%m//%y" date_fmt "%A %d-%m-%Y"
Output: Wednesday 8-11-2023
Step 4: Regenerating the Locale Files
After adjusting the LC_TIME environment variable, you should regenerate the locale files to apply your changes.
$ sudo locale-gen
After regenerating the locale files, you might need to update the system locale configuration to ensure that the changes are taken up by all services and applications:
$ sudo update-locale
Step 5: Making Changes Permanent
To ensure your changes are permanent, either log out of your Linux session and log back in or simply reboot your system. These steps will activate your modified date and time format preferences.
Congratulations, you've successfully customized the date format on your Linux system to match your personal style!
Format codes cheat sheet
Here's a quick-reference cheat sheet for the strftime format codes, suitable for keeping handy when working with date and time formatting in Linux:
- %a: Abbreviated day of the week according to the current locale.
- %A: Full day of the week according to the current locale.
- %b: Abbreviated month name according to the current locale.
- %B: Full month name according to the current locale.
- %c: Preferred date and time representation for the current locale.
- %C: Century number as a 2-digit integer.
- %d: Day of the month as a decimal number (01 to 31).
- %D: Equivalent to %m/%d/%y (use with caution due to international format differences).
- %e: Day of the month as a decimal number with leading zero replaced by a space.
- %E: Modifier for alternative ("era-based") format.
- %F: Equivalent to %Y-%m-%d (ISO 8601 date format).
- %G: ISO 8601 week-based year with century as a decimal number.
- %g: ISO 8601 week-based year without century (2-digit year).
- %h: Equivalent to %b.
- %H: Hour as a decimal number using a 24-hour clock (00 to 23).
- %I: Hour as a decimal number using a 12-hour clock (01 to 12).
- %j: Day of the year as a decimal number (001 to 366).
- %k: Hour (24-hour clock) as a decimal number (0 to 23) with single digits preceded by a space.
- %l: Hour (12-hour clock) as a decimal number (1 to 12) with single digits preceded by a space.
- %m: Month as a decimal number (01 to 12).
- %M: Minute as a decimal number (00 to 59).
- %n: Newline character.
- %O: Modifier for using alternative numeric symbols.
- %p: "AM" or "PM" or corresponding strings according to time value.
- %P: Like %p but in lowercase.
- %r: Time in a.m. or p.m. notation.
- %R: Time in 24-hour notation (%H:%M).
- %s: Number of seconds since the Epoch (1970-01-01 00:00:00 +0000 UTC).
- %S: Second as a decimal number (00 to 60, allowing for leap seconds).
- %t: Tab character.
- %T: Time in 24-hour notation (%H:%M:%S).
- %u: Day of the week as a decimal number (1 to 7, Monday being 1).
- %U: Week number of the current year (00 to 53, starting with the first Sunday as week 01).
- %V: ISO 8601 week number of the current year (01 to 53, with specific rules).
- %w: Day of the week as a decimal number (0 to 6, Sunday being 0).
- %W: Week number of the current year (00 to 53, starting with the first Monday as week 01).
- %x: Preferred date representation for the current locale without the time.
- %X: Preferred time representation for the current locale without the date.
- %y: Year as a decimal number without a century (00 to 99).
- %Y: Year as a decimal number including the century.
- %z: +hhmm or -hhmm numeric timezone offset from UTC.
- %Z: Timezone name or abbreviation.
- %+: Date and time in date(1) format (not supported in glibc2).
- %%: Literal '%' character.