How to Get Yesterday and Day Before Yesterday in Linux

Get yesterday's date in bash on Linux, DST-safe

I think this should work, irrespective of how often and when you run it ...

date -d "yesterday 13:00" '+%Y-%m-%d'

In a unix shell, how to get yesterday's date into a variable?

If you have Perl available (and your date doesn't have nice features like yesterday), you can use:

pax> date
Thu Aug 18 19:29:49 XYZ 2010

pax> dt=$(perl -e 'use POSIX;print strftime "%d/%m/%Y%",localtime time-86400;')

pax> echo $dt
17/08/2010

Get the date (a day before current time) in Bash

Sorry not mentioning I on Solaris system.
As such, the -date switch is not available on Solaris bash.

I find out I can get the previous date with little trick on timezone.

DATE=`TZ=MYT+16 date +%Y-%m-%d_%r`
echo $DATE

How to get yesterday date in Unix

Ksh's printf supports datetime manipulation:

# echo ${.sh.version}
Version AJM 93u+ 2012-08-01
# printf '%(%Y-%m-%d)T\n' today
2015-09-03
# printf '%(%Y-%m-%d)T\n' yesterday
2015-09-02
# printf '%(%Y-%m-%d)T\n' '5 days ago'
2015-08-29
#

Shell Script for Yesterdays Date

I think you want to use -

$ cat test.sh
#!/bin/bash

dd=$(date --date='yesterday' +'%m-%d-%Y')
echo $dd
$ ./test.sh
12-31-2013

or you could use

$ date -d '1 day ago' +'%m-%d-%Y'
12/31/2013

And for tomorrow -

$ date -d '1 day' +'%m-%d-%Y'
01/02/2014

or

$ date --date='tomorrow'
Thu Jan 2 21:25:00 EST 2014

How do I get a date of yesterday in expect

Try this

#!/usr/bin/expect
set yest [ exec /bin/date -d "yesterday" +%Y%m%d]
send_user $yest
exit 1

getting a previous date in bash/unix

Several solutions suggested here assume GNU coreutils being present on the system. The following should work on Solaris:

TZ=GMT+24 date +’%Y/%m/%d’

How do I get yesterday's date in perl?

If you want yesterday's date:

use DateTime qw( );

my $yday_date =
DateTime
->now( time_zone => 'local' )
->set_time_zone('floating')
->truncate( to => 'day' )
->subtract( days => 1 )
->strftime('%Y-%m-%d');

For example, in New York on 2019-05-14 at 01:00:00, it would have returned 2019-05-13.

For example, in New York on 2019-11-04 at 00:00:00, it would have returned 2019-11-03.

Note that ->now( time_zone => 'local' )->set_time_zone('floating')->truncate( to => 'day' ) is used instead of ->today( time_zone => 'local' ) to avoid this problem.


If you want the time a day earlier:

use DateTime qw( );

my $yday_datetime =
DateTime
->now( time_zone => 'local' )
->subtract( days => 1 )
->strftime('%Y-%m-%d %H:%M:%S');

For example, in New York on 2019-05-14 at 01:00:00, it would have returned 2019-05-13 01:00:00.

For example, in New York on 2019-03-10 at 12:00:00, it would have returned 2019-03-09 12:00:00.

For example, in New York on 2019-03-11 at 02:30:00, it would have resulted in an error. 2019-03-10 02:30:00 didn't exist because of the switch to Daylight Saving Time.


If you want the time 24 hours earlier:

use DateTime qw( );

my $yday_datetime =
DateTime
->now( time_zone => 'local' )
->subtract( hours => 24 )
->strftime('%Y-%m-%d %H:%M:%S');

For example, in New York on 2019-05-14 at 01:00:00, it would have returned 2019-05-13 01:00:00.

For example, in New York on 2019-03-10 at 12:00:00, it would have returned 2019-03-09 11:00:00.

For example, in New York on 2019-03-11 at 02:30:00, it would have returned 2019-03-10 01:30:00.



Related Topics



Leave a reply



Submit