Get Yesterday's Date Using Date

Get yesterday's date using Date

Update

There has been recent improvements in datetime API with JSR-310.

Instant now = Instant.now();
Instant yesterday = now.minus(1, ChronoUnit.DAYS);
System.out.println(now);
System.out.println(yesterday);

https://ideone.com/91M1eU

Outdated answer

You are subtracting the wrong number:

Use Calendar instead:

private Date yesterday() {
final Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
return cal.getTime();
}

Then, modify your method to the following:

private String getYesterdayDateString() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
return dateFormat.format(yesterday());
}

See

  • IDEOne Demo

How to get yesterday instance from Calendar?

Try this :

using this code you will get yesterday date always.

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
dateFormat.format(cal.getTime());

Calculate the date yesterday in JavaScript

var date = new Date();

date ; //# => Fri Apr 01 2011 11:14:50 GMT+0200 (CEST)

date.setDate(date.getDate() - 1);

date ; //# => Thu Mar 31 2011 11:14:50 GMT+0200 (CEST)

How to get yesterday's date with Momentjs?

Just like this: moment().subtract(1, 'days'). It will give you the previous day with the same exact current time that is on your local pc.

getting today's and yesterday's date

You can do it using timedelta :

from datetime import datetime, timedelta
yesterday = datetime.today() - timedelta(days=1)

Output will be :

datetime.datetime(2021, 1, 26, 20, 27, 25, 849797)

Python - Get Yesterday's date as a string in YYYY-MM-DD format

You Just need to subtract one day from today's date. In Python datetime.timedelta object lets you create specific spans of time as a timedelta object.

datetime.timedelta(1) gives you the duration of "one day" and is subtractable from a datetime object. After you subtracted the objects you can use datetime.strftime in order to convert the result --which is a date object-- to string format based on your format of choice:

>>> from datetime import datetime, timedelta
>>> yesterday = datetime.now() - timedelta(1)

>>> type(yesterday)
>>> datetime.datetime

>>> datetime.strftime(yesterday, '%Y-%m-%d')
'2015-05-26'

Note that instead of calling the datetime.strftime function, you can also directly use strftime method of datetime objects:

>>> (datetime.now() - timedelta(1)).strftime('%Y-%m-%d')
'2015-05-26'

As a function:

from datetime import datetime, timedelta

def yesterday(frmt='%Y-%m-%d', string=True):
yesterday = datetime.now() - timedelta(1)
if string:
return yesterday.strftime(frmt)
return yesterday

example:

In [10]: yesterday()
Out[10]: '2022-05-13'

In [11]: yesterday(string=False)
Out[11]: datetime.datetime(2022, 5, 13, 12, 34, 31, 701270)

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