| by Arround The Web | No comments

How to Find Yesterday Date in Bash

Bourne Again Shell also known as Bash comes with various command line utilities to manipulate and automate tasks. One such utility is the date command, which is used to get the date/time and manipulate it. Using the date command, you can also get yesterday’s date to use it in Bash scripts.

Getting yesterday’s date can be useful in order to generate reports, data management, and scheduling tasks.

In this tutorial, I will explore how to use the date command to get yesterday’s date in Bash.

What is the Bash date Command
How to Find Yesterday’s Date in Bash on Linux
1. Finding Yesterday’s Date using -d or –date Options

Finding Yesterday’s Date using the EPOCHSECONDS
How to Find Yesterday’s Date in Bash on macOS
Conclusion

What is the Bash date Command

The date command in Bash displays the date and time in the terminal. It can also be used to set, change, or print the date and time in different formats. The date command is very useful for automating and scheduling tasks in Bash scripting.

To get the current date and time, simply type the date in the terminal:

date

 

To get the date in YYYY-MM-DD format use the following command:

date +"%Y/%m/%d"

 

You can use dashes (-) in the place of slashes (/) in the above command.

Commonly used format control characters are listed below:

%d Displays the day of the month; for example: 02 or 03.
%D Displays the date in Month-Day-Year format.
%m Displays the month; for example: 01-12
%u Displays the day of the week: for example: 1-7. 1 is Monday, 4 is Thursday.
%W Displays the week number of the year. For example, 00-53, 00 being the first week of the year, 01 is the second week of the year.
%j Displays the day of the year; for example, 001 is the first day of the year 366 is the last day.

For more details, execute the following command to access the manual page of the date command:

man date

 
Another important option that comes with the date command is -d or –date. Using these options, we can get previous dates, future dates, and even yesterday’s. These options are used to display time given in the form of strings. For example:

date --date "last Saturday"

 

Or:

date -d "last Saturday"

 

What are the other strings that -d or –date options can accept are mentioned below:

    • tomorrow
    • yesterday
    • last Saturday
    • next Saturday
    • last week
    • next week
    • last year
    • next year
    • X sec ago
    • X day ago
    • X week ago
    • X year ago
    • X days
    • X week
    • X year

In the next section, I will use -d and –date options to find yesterday’s date.

How to Find Yesterday’s Date in Bash on Linux

To find yesterday’s date in Bash there are multiple ways. I will discuss each method:

    • Finding Yesterday’s Date using -d or –date Options
    • Finding Yesterday’s Date using the EPOCHSECONDS

Note: Finding yesterday’s date depends upon the system’s date capabilities.

1. Finding Yesterday’s Date using -d or –date Options

To get yesterday’s date in Bash use the following command:

date -d "1 day ago"

 

Or use:

date --date "1 day ago"

 

The 1 day ago string can also be replaced with -1 day.

date --date "-1 day"

 

You can also format the date using format characters.

date --date "1 day ago" +"%Y/%m/%d"

 

Another way to get yesterday’s date in bash is using the yesterday string with the -d or –date options.

date --date "yesterday"

 

To format it in YYYY/MM/DD use the following command:

date --date "yesterday" +"%Y/%m/%d"

 
What are the Limitations of the -d or –date Options in Linux

The -d or –date options give you yesterday’s date but these options have some limitations. These options do not consider Daylight Saving Time or DST while giving the output and rely on the current time zone. The date command simply subtracts the 24 hours from the current date and time to give yesterday’s output which eliminates the DST consideration.

One approach to avoid such a situation is using the UTC (-u flag) time, which does not rely on the local time zone to calculate yesterday’s date and gives the output according to a fixed time zone. Use the -u flag with the date command:

date -u --date "yesterday" +"%Y-%m-%d"

 

2. Finding Yesterday’s Date using the EPOCHSECONDS

Yesterday’s date can also be found using the EPOCHSECONDS. To find yesterday’s date execute the following command:

printf -v yesterday '%(%Y/%m/%d)T' $(( EPOCHSECONDS – 86400 ))

 
To print the yesterday’s date run:

echo $yesterday

 

This command calculates the date of the previous date using EPOCHSECONDS and assigns it to the yesterday variable in YYYY-MM-DD format. The EPOCHSECONDS is the number of seconds since January 1, 1970, where 86400 signifies the number of seconds per day.

How to Find Yesterday’s Date in Bash on macOS

Although macOS comes with Zsh as the default shell, many still prefer Bash which is widely used. The date command in macOS does not support the yesterday syntax, therefore the procedure of getting yesterday’s date on macOS is a bit different.

In macOS, the date command is used to find yesterday’s date.

date -v-1d

 

To get yesterday’s date in a specific format on macOS use:

date -v-1d +"%Y/%m/%d"

 

The -v flag is used to adjust the time and date; for example: if you want a date of the previous day, use 1d with a minus () sign. To get the date for the next day simply use the plus (+) sign. Here d represents days from 1-31. For more details, execute the following command in the macOS bash terminal:

man date

 
Note: The -v flag is specific to macOS and may not work on other operating systems such as Linux.

Conclusion

You need to work with date and time in order to schedule tasks, create reports, and back up data as a system administrator. In order to get yesterday’s date in the Linux system you can use the date command with -d or –date flags. In macOS also the date command is used but with a -v flag and a positive or negative integer with d character.

Share Button

Source: linuxhint.com

Leave a Reply