What is the Linux Date Command? Learn to Set, Format, Change Date
Introduction
It is of utmost importance to ensure that the correct date and time are accurately set on any Unix-like system, especially those designed for multitasking. While Linux has an advantage with its synchronization to the system’s hardware clock, certain issues may arise, causing the hardware clock to display an outdated time.
The Linux Date Command proves to be extremely useful to prevent discrepancies in time synchronization and to ensure the generation of precise timestamps for log entries.
This article aims to provide a comprehensive guide on various methods of using the Linux date command, offering clear examples and syntax and a comprehensive overview of date formats. Without further delay, we will look into the intricacies of the date command.
Linux Date Command Format Options
Whether you want the year to be written in two digits (e.g., 93) or four digits (e.g., 1993), a date command has every option. You can choose to show the hour in a 24-hour format (e.g., 14) or a 12-hour format (e.g., 01). A date command has a wide variety when it comes to formats. This wide variety makes the Linux date command flexible, scalable, and customizable for the client. Let’s scan out some prevalent formats of this command.
%a: Display the abbreviated weekday name (e.g., Tue)
%A: Display the full weekday name (e.g., Tuesday)
%b: Display the abbreviated month name (e.g., Mar)
%B: Display the full month name (e.g., March)
%c: Display the date and time (e.g., Fri Mar 11 23:05:25 1995)
%C: Display the century; omit the last two digits (e.g., 19)
%d: Display the day of the month (e.g., 05)
%D: Display date; same as %m/%d/%y (e.g., 07/10/23)
%e: Display the day of the month with optional padding (e.g., 7)
%F: Display the full date; same as YYYY/MM/DD (e.g., 1961/08/01)
%g: Display the last two digits of the year as per the ISO 8601 week-based year format (e.g., 23
for the current year 2023)
%G: Display the year of ISO week number (e.g., 2023)
%h: The result is similar to the ‘%b’ option
%H: Utilized for an hour (00 to 23)
%I: Used for an hour (01 to 12)
%j: Used for the day of the year (001 to 366)
%k: Utilized for the hour, space padded ( 0..23); same as %_H
%l: Used for the hour, space padded ( 1..12); same as %_I
%m: Used for the month (01..12)
%M: Employed for the minute (00..59)
%n: Used for a new line
%N: Utilized for nanoseconds (000000000..999999999)
%p: AM/PM indicator (e.g., AM or PM)
%P: The output is similar to the ‘%p’ option, but the focus is on lowercase (e.g., am or pm)
%q: Employed display the quarter of a year (1 to 4)
%r: Utilized to display the 12-hour clock time (e.g., 11:11:04 PM)
%R: Used to display the 24-hour clock time in an hour and minute; same as %H:%M
%s: Displays the seconds since 1970-01-01 00:00:00 UTC
%S: Used to display the second (00 to 60)
%t: Used for a tab option.
%T: Utilized to display the time; same as %H:%M:%S (e.g., 2:30:45 PM)
%u: Employed for the day of the week (1 to 7); 1 is Monday
%U: Used for a week number of the year; it counts Sunday as the first day of the week (00..53)
%V: Displays the ISO week number; it counts Monday as the first day of the week (01..53)
%w: Used for the day of the week (0..6); 0 is Sunday
%W: Displays week in number; it counts Monday as the first day of the week (00..53)
%x: Used for the date representation (e.g., 12/31/99)
%X: Used for the time representation (e.g., 23:13:48)
%y: Employed for the last two digits of the year (00..99)
%Y: Utilized for a year
%z: Used for the +hh:mm numeric time zone (e.g., -0400)
We will now take some of these formats to make a certain command.
date "+DATE: %D%nTIME: %T"
Result – Date: 08/18/61
Time: 4:30:44
Here %D represents date i.e., 08/18/61 and %T represents time i.e., 4:30:44
Also Read: 50+ Linux Commands with Screenshots (Download PDF)
How to Set Linux Date Command with Example
To adjust the date in Linux using the ‘date’ command, it is necessary to have superuser privileges or utilize the ‘sudo’ command. The access is vital as the modification of the system date is not open for all. Below is an instance of how to employ the ‘date’ command to fix the date.
sudo date -s "YYYY-MM-DD HH:MM:SS"
To specify the exact date and time you would like to set, you will need to replace the placeholder “YYYY-MM-DD HH:MM:SS” with your chosen date and time format. It’s essential to track the pattern of “YYYY-MM-DD HH:MM:SS”. For instance, if you prefer to set a date for July 10, 2023, at 2:30 PM, then you can use this command.
sudo date -s "2023-07-10 14:30:00"
After the execution of the command, the date and time of the system will be modified accordingly. It should be kept in mind that altering the date and time usually necessitates administrative privileges; hence the command must be preceded by the “sudo” keyword.
Date option for displaying past dates with syntax
To display past dates using the date command in Linux, simply use the -d option to specify a specific date or relative date expression. Here’s the exact syntax you need.
date -d "EXPRESSION" "+FORMAT"
- Display a specific past date:
date -d "2022-01-01" "+%Y-%m-%d"
To format the date “2022-01-01” in the desired “YYYY-MM-DD” style, simply enter this command.
Date option for displaying Future dates with syntax
To display future dates using the date command in Linux, you can use the -d option with a specific date or a relative date expression. Here’s the syntax.
date -d "EXPRESSION" "+FORMAT"
Display a specific future date.
date -d "2024-12-31" "+%Y-%m-%d"
This command will display the date “2024-12-31” in the format “YYYY-MM-DD”.
Display Last Modified Timestamp of a Date File
Determining when a file was last modified in Linux is a breeze with the “ls” command and the “-l” option to display its long format. Allow me to provide you with an example of how to do it.
ls -l --time-style=+"%Y-%m-%d %H:%M:%S" FILENAME
It’s easy to check a file; to do so, simply replace “FILENAME” with the file’s actual name. If you want to see the components of the file in a more complete format? Simply use the “-l” option. This will display the file’s permissions, owner, group, size, and timestamps.
To format the timestamps, use the –time-style option and determine the preferred format. In this example, we are using “%Y-%m-%d %H:%M:%S” to display the last modified timestamp as “YYYY-MM-DD HH:MM:SS”. For example, to show the last modified timestamp of the file “example.txt”, use this command.
ls -l --time-style=+"%Y-%m-%d %H:%M:%S" example.txt
The output will show the file details, including the last modified timestamp, like:
-rw-r--r-- 1 user group 4096 2023-07-10 13:45:30 example.txt
This example indicates that the file “example.txt” was last modified on July 10th, 2023, at 1:45:30 PM.
Use Unix Epoch Time (Convert Epoch Time)
To easily determine the last modification time of a file in Unix Epoch Time, you can utilize the stat command with the %Y format code. Allow us to furnish a simple example.
stat -c "%Y" FILENAME
If you want to check a specific file, you must replace “FILENAME” with its actual name. To customize the output format for the statistics, you can use the -c option. The %Y format shows the last time the file was modified in Unix Epoch Time format. This format measures time in seconds from January 1, 1970, 00:00:00 UTC, providing a precise and standardized way to record file changes.
Also Read: How to Use the xargs Command in Linux?
Conclusion
The Linux Date Command is an incredibly powerful tool for managing and manipulating date and time in a Linux environment. With this command, you can display the current date and time, set the system clock, format dates and times, calculate future and past dates, and so much more. Additionally, the date command comes with a wide variety of format options, meaning you can customize the result per your requirements.
This article examines different elements of the date command, including how to set, format, and change options. We have explored how to display different date and time components, such as the year, month, day, hour, and minute, using format codes like %Y, %m, %d, %H, and %M. We have also covered options for displaying the weekday, month name, and various timestamp formats.