Uncategorized

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)

Display the abbreviated weekday name

%A: Display the full weekday name (e.g., Tuesday)

Display the full weekday name

%b: Display the abbreviated month name (e.g., Mar)

Display the abbreviated month name

%B: Display the full month name (e.g., March)

Display the full month name

%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)

Display the century; omit the last two digits

%d: Display the day of the month (e.g., 05)

Display the day of the month

%D: Display date; same as %m/%d/%y (e.g., 07/10/23)

Display date; same as %m/%d/%y

%e: Display the day of the month with optional padding (e.g., 7)

Display the day of the month with optional padding

%F: Display the full date; same as YYYY/MM/DD (e.g., 1961/08/01)

Display the full date; same as YYYY/MM/DD

%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)

Display the last two digits of the year

%G: Display the year of ISO week number (e.g., 2023)

 Display the year of ISO week number

%h: The result is similar to the ‘%b’ option

The result is similar to the '%b' option

%H: Utilized for an hour (00 to 23)

Utilized for an hour

%I: Used for an hour (01 to 12)

Used for an hour (01 to 12)

%j:   Used for the day of the year (001 to 366)

 Used for the day of the year

%k:  Utilized for the hour, space padded ( 0..23); same as %_H

Utilized for the hour, space padded

%l:   Used for the hour, space padded ( 1..12); same as %_I

 Used for the hour, space padded

%m: Used for the month (01..12)

Used for the month

%M: Employed for the minute (00..59)

Employed for the minute

%n: Used for a new line

Used for a new line

%N: Utilized for nanoseconds (000000000..999999999)

Utilized for nanoseconds

%p: AM/PM indicator (e.g., AM or PM)

AM/PM indicator

%P: The output is similar to the ‘%p’ option, but the focus is on lowercase (e.g., am or pm)

 The output is similar to the '%p' option, but the focus is on lowercase

%q: Employed display the quarter of a year (1 to 4)

Employed display the quarter of a year

%r: Utilized to display the 12-hour clock time (e.g., 11:11:04 PM)

Utilized to display the 12-hour clock time

%R: Used to display the 24-hour clock time in an hour and minute; same as %H:%M

Used to display the 24-hour clock time in an hour

%s: Displays the seconds since 1970-01-01 00:00:00 UTC

Displays the seconds since 1970-01-01 00:00:00 UTC

%S: Used to display the second (00 to 60)

Used to display the second (00 to 60)

%t: Used for a tab option.

Used for a tab option.

%T: Utilized to display the time; same as %H:%M:%S (e.g., 2:30:45 PM)

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

Employed for the day of the week (1 to 7)

%U: Used for a week number of the year; it counts Sunday as the first day of the week (00..53)

Used for a week number of the year

%V: Displays the ISO week number; it counts Monday as the first day of the week (01..53)

Displays the ISO week number

%w: Used for the day of the week (0..6); 0 is Sunday

Used for the day of the week

%W: Displays week in number; it counts Monday as the first day of the week (00..53)

Displays week in number

%x: Used for the date representation (e.g., 12/31/99)

Used for the date representation

%X: Used for the time representation (e.g., 23:13:48)

Used for the time representation

%y: Employed for the last two digits of the year (00..99)

Employed for the last two digits of the year

%Y: Utilized for a year

Utilized for a year

%z: Used for the +hh:mm numeric time zone (e.g., -0400)

Used for the +hh:mm numeric time zone

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"
Set Linux Date Command

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"
Date option for displaying past dates with syntax 

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"
Date option for displaying past dates with syntax 

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
Display Last Modified Timestamp of a Date File 

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
Display Last Modified Timestamp of a Date File 

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
Use Unix Epoch Time

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.

Leave a Reply

Your email address will not be published. Required fields are marked *