How to Display Dates and Times on Linux: A Comprehensive Guide
The Linux date command is extremely versatile. You can use it to display dates, times, and select date-related values (like the day of the week or time zone) with very little effort. The default date command output will display the current date and time in a fairly detailed string that includes the day of the week, the current time, and the time zone. Here’s an example:
If you want to see details on a date in the past or in the future, use the -d option. For example, maybe you want to know what day of the week your birthday is going to fall on next year. You could use a command like this:
If you want to see a particular version or element of a date, use the characters described in the list of options below.
To use one of these options with the date command, precede it with a + sign. For example, the command below shows the date in mm/dd/yy format:
To use more than one option at a time, enclose the arguments in quote signs like this command that shows the day of week and date:
A command like the one below will display only the day of the week, but fully spelled out:
If you want the month spelled out fully, use a command like this one:
The date command also allows you to show dates in the future. While you likely know what day tomorrow is, you could use a similar command to display the current time but the next day.
This next command shows the day of the week 2 years in the future.
The next command shows the full date.
You can also use the date command to display dates and times in the past. The commands below show yesterday’s date, the date and time two hours ago, and the day of the week from two years ago today.
If you want to display date and time information in a multi-line display, insert a newline (%n) string into your date command where the break should occur. Here’s an example:
If you want to insert date/time information into files, use > to create/overwrite the file or >> to add the data as in these examples:
The TZ option will allow you to specify the time zone that you want to see displayed. So, if you want to see what time it is on the US West Coast when you’re on the US East Coast, you could use a command like this:
Time zones are defined for hundreds of locations. Here are a few examples:
To list available time zones (about 600 of them), you can use a command like one of those shown below. Keep in mind, however, that this command shows time zones for each city or area. This level of detail is generally more than needed since most cities also share more generic time zones with many other cities.
You can also use the three-character time zone abbreviations when they are available. Here are some examples:
The most unusual way to display the current time on Linux is to use the date +%s command. This shows the current time as the number of seconds since 00:00:00 on Jan 1, 1970, and is also referred to as the “epoch time”. Since this was designated to be Linux’s birth date and is the way dates are stored internally, it works quite well.
To see how many seconds have passed since a particular date, use a command like this:
Don’t overlook the fact that you can turn any date command into an alias if it saves you time and trouble. If you frequently need to display the current date and time in some other location, add an alias to your .bashrc file like this:
Once you source your .bashrc file or log in next time, it should work. I found a list of time zone abbreviations at this URL: https://www.timeanddate.com/time/zones/
The date command provides lots of ways to see dates and time, presented in various formats, in the past or future, and depending on location. For more posts related to dates and times, check out:
Welcome to DediRock, your trusted partner in high-performance hosting solutions. At DediRock, we specialize in providing dedicated servers, VPS hosting, and cloud services tailored to meet the unique needs of businesses and individuals alike. Our mission is to deliver reliable, scalable, and secure hosting solutions that empower our clients to achieve their digital goals. With a commitment to exceptional customer support, cutting-edge technology, and robust infrastructure, DediRock stands out as a leader in the hosting industry. Join us and experience the difference that dedicated service and unwavering reliability can make for your online presence. Launch our website.