How to Get Yesterday's Date in Linux

Olorunfemi Akinlua Feb 02, 2024
  1. Use the date Command to Get Yesterday’s Date in Linux
  2. Use the tz Variable to Get Yesterday’s Date in Linux
How to Get Yesterday's Date in Linux

Bash is a popular shell and command-line interface for Linux, and it provides many valuable tools and features for working with dates and times. In this article, we will explore several different approaches for getting yesterday’s date in Bash on Linux, and we will also discuss how to ensure that the date is DST-safe (daylight saving time safe).

Use the date Command to Get Yesterday’s Date in Linux

One of the most straightforward ways to get yesterday’s date in Bash is to use the date command. This command allows you to format the output of the current date and time in many different ways, and you can use it to display the date in the format you need.

For example, to get yesterday’s date in the format YYYY-MM-DD, you can use the following command:

date -d "yesterday" +%F

Output:

2022-11-26

This command uses the -d option to specify a date string (in this case, "yesterday") and the +%F format specifier to print the date in the YYYY-MM-DD format. This will return yesterday’s date in the specified format, such as 2022-11-26 for November 26, 2022.

However, this approach has some limitations. For example, it relies on the local time zone of the system, and it does not account for daylight saving time (DST).

This means that the result may not be accurate if the date falls within a DST transition period. To solve this problem, we can use a different approach considering DST.

A way to do this is to use the -u option with the date command, which tells it to use the UTC zone instead of the local time zone. This will ensure that the date is calculated using a fixed time zone and avoid any issues with DST transitions.

Here’s an example of using this option to get yesterday’s date in the YYYY-MM-DD format.

date -u -d "yesterday" +%F

Output:

2022-11-26

This command is similar to the previous one but adds the -u option to use the UTC zone. This will return yesterday’s date in the specified format, but it will be calculated using the UTC zone instead of the local time zone.

Use the tz Variable to Get Yesterday’s Date in Linux

In Bash, the tz environment variable is not standard. It is a user-defined variable that can be used to store the time zone for the system.

This value can then be used by programs that rely on the time zone to calculate or display dates and times.

For example, the TZ variable can be set to a time zone string such as "America/Los_Angeles" or "Europe/Paris" to specify the desired time zone. Note that the format and contents of the time zone string may vary depending on the system and the time zone database it uses.

We can use the tz variable and the $EPOCHSECONDS variable to obtain yesterday’s date.

printf -v tz '%(%z)T' -1
TZ=$tz printf -v yesterday '%(%Y-%m-%d)T' $(( EPOCHSECONDS - 86400 ))
echo $yesterday

Output:

2022-11-26

The code sets the system’s time zone and prints the previous day’s date in the specified format (YYYY-MM-DD). The -v flag for the printf command assigns the output of the command to a variable (tz for the time zone and yesterday for the date of the previous day).

The -1 argument for the first printf command specifies the system’s current time zone. The second printf command uses the TZ environment variable to set the time zone and the $(( POCHSECONDS - 86400 )) expression to calculate the date of the previous day in epoch time (the number of seconds that have elapsed since January 1, 1970).

The %(%Y-%m-%d)T format string specifies the desired date format, and the echo command prints the date to the terminal.

Olorunfemi Akinlua avatar Olorunfemi Akinlua avatar

Olorunfemi is a lover of technology and computers. In addition, I write technology and coding content for developers and hobbyists. When not working, I learn to design, among other things.

LinkedIn

Related Article - Bash Date