Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get yesterday in python [duplicate]

Tags:

python

To get now, I can do:

now = datetime.datetime.now()

How would I get 24 hours ago?

now - 24 hrs. ?
like image 628
David542 Avatar asked Nov 05 '13 00:11

David542


People also ask

How do I get yesterday day in Python?

Use datetime. today() to get today's date in local time. Use today - datetime. timedelta(days=1) to subtract one day from the previous result today .

How do I get yesterday's timestamp?

To get yesterday's date, you need to subtract one day from today. Use current_date to get today's date. In Oracle, you can subtract any number of days simply by subtracting that number from the current date.

How do I get today's day in Python?

now() – How to Get Today's Date and Time. You can use the datetime module in Python to retrieve data about date and time.

How do you get the second date in Python?

To convert a datetime to seconds, subtracts the input datetime from the epoch time. For Python, the epoch time starts at 00:00:00 UTC on 1 January 1970. Subtraction gives you the timedelta object. Use the total_seconds() method of a timedelta object to get the number of seconds since the epoch.


2 Answers

Use timedelta:

datetime.datetime.now() - datetime.timedelta(days=1)

http://docs.python.org/2/library/datetime.html#timedelta-objects

like image 117
David542 Avatar answered Nov 07 '22 10:11

David542


you could use:

datetime.date.fromordinal(datetime.date.today().toordinal()-1)

Get yesterday's date in Python, DST-safe

this will help with formatting:

http://www.cyberciti.biz/faq/howto-get-current-date-time-in-python/

like image 11
Plasmarob Avatar answered Nov 07 '22 11:11

Plasmarob