To get now, I can do:
now = datetime.datetime.now()
How would I get 24 hours ago?
now - 24 hrs. ?
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 .
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.
now() – How to Get Today's Date and Time. You can use the datetime module in Python to retrieve data about date and time.
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.
Use timedelta
:
datetime.datetime.now() - datetime.timedelta(days=1)
http://docs.python.org/2/library/datetime.html#timedelta-objects
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With