In Linux, the date command is used to display and set the system date and time. The date command has various options that allow you to format the output and set the date and time.
The date command is a standard Unix/Linux command that provides information about the current date and time. It is commonly available on most web hosting environments that run on Unix-like operating systems.
Table of Contents
Display Current Date and Time in Linux
To display the current date and time in Linux using the date command, you can simply enter the following command in the terminal:
date

Display Time in GMT
To display the current time in GMT (Greenwich Mean Time) using the date command in Linux, you can use the --utc or -u option.
date -u

Display the Given Date String in the Format of Date
To display a given date string in a specific format using the date command in Linux, you can use the -d option to specify the input date string and the + option to specify the desired output format.
date --date=" string "
date --date="Jan 29 2024"

Display Past Dates
To display past dates in Linux using the date command, you can use the -d option to specify a starting point and then subtract a certain number of days, months, or years
Date and time of 1 years ago
date --date="1 year ago"

Date and time of 5 seconds ago
date --date="5 sec ago"

Date and time of previous day
date --date="yesterday"

Display Future Dates
To display future dates in Linux using the date command, you can use the -d option to specify a starting point and then add a certain number of days, months, or years
date --date="next tue"

date --date="2 day"

Set the System Date and Time
Setting the system date and time in Linux requires using the date command with appropriate options.
However, adjusting the system date and time typically requires superuser (root) privileges, so you may need to use sudo or switch to the root user.
date --set="Tue Jan 30 15:23:34 UTC 2018"

Display Date Strings from a File
If you want to display date strings from a file in Linux, you can use a combination of commands, such as cat to read the contents of the file and date to format and display the dates.
date --file=datefile
cat >> datefile

date --file=datefile

Display Last Modified Timestamp of a File
To display the last modified timestamp of a file in Linux, you can use the stat command. The stat command provides detailed information about a file, including its timestamps.
The -r option is used to display the last modified timestamp of a specified file.
date -r file.txt
We can modify the timestamp of a datefile by using touch command.
touch datefile

Format specifiers used with date command
The date command in Linux uses format specifiers to define how the date and time information should be displayed. Below are some commonly used format specifiers with the date command:
- %Y: Year with century as a decimal number (e.g., 2024).
- %y: Year without century as a decimal number (00 to 99).
- %m: Month as a decimal number (01 to 12).
- %d: Day of the month as a decimal number (01 to 31).
- %H: Hour (00 to 23).
- %I: Hour (01 to 12).
- %M: Minute (00 to 59).
- %S: Second (00 to 59).
- %A: Full weekday name (e.g., Sunday).
- %a: Abbreviated weekday name (e.g., Sun).
- %B: Full month name (e.g., January).
- %b or %h: Abbreviated month name (e.g., Jan).
- %c: Date and time representation (e.g., “Thu Jan 29 15:30:00 2024”).
- %x: Date representation (e.g., “01/29/24”).
- %X: Time representation (e.g., “15:30:00”).
You can use these format specifiers with the date command to customize the output according to your preferences.
date +%[format-option]
date "+%X"

We hope you’ve found this useful.




