Date Command in Linux

Photo of author

By admin

When using the Linux command line or writing Shell scripts, we may need to deal with date and time variables, such as displaying a date or time in a certain format, obtaining a relative date-time, or modifying the device date and time.

The Date command in Linux displays and changes the current date and time on the system. Users can also display the time in various formats and compute upcoming and previous dates using this function. The blog explains how to format dates in Linux and Unix platforms.

Date Command in Linux

Because the Date command is part of the GNU Coreutils package, it comes preinstalled in all Linux distributions. Before proceeding, consider the following prerequisites:

  • An individual should have a Linux-based system.
  • A user account with administrative powers.
  • A terminal window/command line is required.

The date command has the following syntax:

date [option]… [+format]

If no option is given with the command, then the output will be the current system’s date and time, including the day of the week, month, time, year, and timezone:

$ date

Tue 13 Jul 2021 05:34:45 PM CET

The Date command can however do more if we supply other options to it. The date command is commonly used in the following ways:

  • Showing the current date and time in a particular timezone
  • Showing the date and time in the desired format
  • Displaying date and time in the given strings
  • Getting relative date and time
  • Setting the device date and time

How to Use date Command in Linux

Enter the following date command to see the current system time and date:

Date

The weekday, month, year, current time, and time zone are all displayed in the output. The date command is configured for the operating system’s time zone by default. Users can utilize the -d option for operating on a given date. We can enter the following command, for instance:

date -d “2000-11-22 09:10:15”

The —Date command displays a date in the format specified by the date string. This command has no effect on the system’s actual date and time values; it merely prints the date you choose. Consider the following scenario:

date –date=”09/10/1960″

Linux Date Command Format Options

Control characters followed by a + sign can be used to modify the date command’s output. The percent symbol precedes the format controls, which are then replaced by the current values. The % Y symbol is substituted with the present year, the month is swapped with the month, and the day of the month is replaced with the actual day:

date +”Year: %Y, Month: %m, Day: %d”

Nearly every formatting characters for date command is listed below:

%FORMAT String Description
%a The abbreviated weekday name of the location. For example- Tue
%A The entire weekday name of the location For example- Tuesday
%b Shortened month name of the location. For example- Feb
%B The whole month name of the location, such as February
%c Date and time. For instance- Thu Mar 3 23:05:25 2005)
%d day of the month, for instance- 04
%D Exact date. Similar to %m/%d/%y
%F Mentioning complete date such as “%Y-%m-%d”
%g Two digits from the end of ISO week-numbering
%H Showing 24 hours format
%I Showing 12 hours format
%j Displaying the day of the year
%m Month (In number between 1 to 12)
%M Showing minute (Can be any number between 1 to 59)
%n Adding a newline
%j Mentioning the day of the year (Can be any number from 1 to 366)
%N Displaying nanoseconds (Can be any number between 000000000..999999999)
%p AM or PM in your locale; leave blank if you don’t know.
%P Similar to %p, but displays the result in lower case
%S Adding seconds (between 0 to 60)
%t A tab
%T Adding time, similar to %H:%M:%S
%u Displaying day of a week (from 1 to 7)
%U Mentioning the week number. Can be from 1 to 53, because there are 53 weeks in a year
%V ISO week number
%w Mentioning the day of the week
%x Locale’s format of representing date. For example- 12/31/99
%X Locale’s format of representing date. For example- 12:31:99
%y Two digits from the end (In year)
%Y Displaying the year
%z +hhmm numeric time zone
%:z +hh:mm numeric time zone
%::z +hh:mm:ss numeric time zone
%:::z numeric time zone with “:” for more precise information
%Z acronym for the time zone in alphabetical order

Setting or Changing Date in Linux

Use the —set command to manually modify the system clock. For setting the date and time to 4:20 p.m. on July 13, 2014, for instance, enter:

date –set=”20100513 05:30″

The system clock in many of those Linux distributions is synced via the ntp or systemd-timesyncd services, therefore be cautious when manually changing the clock.

Display Past Dates

To see dates from the previous era in Linux, utilize the —date command. “Tomorrow,” “Friday,” “last Friday,” “next Friday,” “next week,” and some other values are accepted by the date command. For printing previous dates, execute the following strings:

date –date=”X year ago”

Show Upcoming Dates

The —date option can also be used to show dates in the future. You can enter in strings to print upcoming dates, just like you can with past dates:

date –date=”next monday”

Displaying the date strings at the file’s line

The —file command displays the date string that appears at the beginning of each line in the file. The —file option, unlike the —date option, can show multiple date strings on every line. The —file command has the following syntax:

date –file=file_name.txt

Show the Date File’s Last Modified Timestamp

The date command reports the last change time of a file if you specify it with the -r option. The below-mentioned command will print the last instance when the host’s file get changed:

date -r /etc/hosts

Overriding a Time Zone

The date command utilises the local time specified in /etc/localtime by default. Set the TZ parameter to the preferred standard time to utilise a different time zone in the environment. To change to New York time, for example, type:

TZ=’America/New_York’ date

To reset the machine to its original time zone, use the date command. Use the timedatectl list-timezones command to examine all possible time zones.

The date command can be utilised for displaying the time in another local time. For example, to see the local time on the Australian East coast at 11:20 pm upcoming Monday, enter:

date -d ‘TZ=”Australia/Sydney” 11:20 next Monday’

Using Date with Other Commands

The Date command can be used to generate system files that include the current time and date. The following command produces a duplicate MySQL file in the present date format:

mysqldump database_name > database_name-$(date +%Y%m%d).sql

Shell scripts are another typical application of the Date command. The output of date is assigned to the date now variable as follows:

date_now=$(date “+%F-%H-%M-%S”)

Conclusion

The Date command displays or modifies the date and time on the system in Linux. In addition to displaying the time in various formats, this function can also be used to calculate upcoming and previous dates. This blog discusses how the Date command can be used in Linux in all of its major uses. I hope you found this information useful.

Leave a Comment