Obtain the datetime object for the previous day at midnight [duplicate] - python

This question already has answers here:
What was midnight yesterday as an epoch time?
(7 answers)
Closed 5 years ago.
I have a datetime object in Python, for, let's say, 25/06/2017 11:22:33. I'd like to find a pythonic way of getting a datetime object from that one that would represent 24/06/2017 00:00:00.
I can think of:
day_before = now - datetime.timedelta(days=1,
hours=now.hour,
minutes=now.minute,
seconds=now.second)
But I was wondering if there is a more concise way.

from datetime import date, timedelta
yesterday = date.today() - timedelta(1)
From here.

Related

how to calculate date after 8 months from delivery_date? [duplicate]

This question already has answers here:
How do I calculate the date six months from the current date using the datetime Python module?
(47 answers)
Closed 1 year ago.
I want to make a function that returns the expiration date.
this is the field to calculate from?
delivery_date = models.DateTimeField(auto_now_add=True)
this is how to do it in python:
from datetime import date
from dateutil.relativedelta import relativedelta
date_of_interest = date.today() # get this from your app
eight_months = date_of_interest + relativedelta(months=+8)
print(date_of_interest)
print(eight_months)
from:
How do I calculate the date six months from the current date using the datetime Python module?

How to get previous month from a str datetime in Python? [duplicate]

This question already has answers here:
python date of the previous month
(18 answers)
Finding the previous month
(5 answers)
Closed 2 years ago.
I have a str variable which is my_variable = str(201705).
I want to get the previous month of my_variable using datetime in python. Is there a method to accomplish this? So far I have:
from datetime import datetime
my_variable = str(201705)
month = datetime.strptime(my_variable, '%Y%m').strftime("%b%Y")
This gives me my current variables month in str format, so May2017. I want to get just the previous month stored in a new variable. So my_new_month would = April.
Is this possible using the python datetime library, and if not, is there another solution to accomplish this?
from datetime import datetime, timedelta
my_variable = str(202102)
d1 = datetime.strptime(my_variable, '%Y%m')
days = d1.day
# use timedelta to subtract n+1 days from current datetime object
d2 = d1 - timedelta(days=days+1)
# get month of d2
print(d2.month)

How to convert HH:MM:SS to time.time() object in Python [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 2 years ago.
I need to convert HH:MM:SS format to time.time() object. Is there any way to do it?
Here's my HH:MM:SS format time:
a = '14:37:29'
I want to convert it to time.time() object such as:
a = 1600256249
Is this achievable? If it's not, what should I try? Hope you help.
To get UNIX time, you need to add a date. For example, you could combine your time string with today's date:
from datetime import datetime, timezone
s = '14:37:29'
today = datetime.today() # 2020-09-16
# make a datetime object with today's date
dt = datetime.combine(today, datetime.strptime(s, '%H:%M:%S').time())
# make sure it's in UTC (optional)
dt = dt.replace(tzinfo=timezone.utc)
# get the timestamp
ts = dt.timestamp()
print(ts)
# 1600267049.0
You could also set other time zones with this approach using dateutil or zoneinfo (Python 3.9+).
Is this achievable?
I would say no. a = '14:37:29' holds only hour-minute-second, whilst time.time() does return seconds since start of epoch i.e. you would also need to known day, month and year beside hour, minute, second, to create equivalent of what time.time() returns.

Calculate difference between two datetimes odoo 10 [duplicate]

This question already has answers here:
Date difference in minutes in Python
(12 answers)
Closed 5 years ago.
Working on Odoo10, i need to calculate the difference between two fields of datetime type, start and finish, i need the difference to be in minutes. how can i do that ?
Try with this example:
from dateutil.relativedelta import relativedelta
#api.one
#api.depends('start_field','finish_field')
def _total_minutes(self):
if self.start_field and self.finish_field:
start_dt = fields.Datetime.from_string(self.start_field)
finish_dt = fields.Datetime.from_string(self.finish_field)
difference = relativedelta(finish_dt, start_dt)
days = difference.days
hours = difference.hours
minutes = difference.minutes
seconds = 0

How do I change a date to YYYYMMDD in Python? [duplicate]

This question already has answers here:
Getting today's date in YYYY-MM-DD in Python?
(12 answers)
Closed 6 years ago.
I have the below code which works out the date 6 months ago from todays date.
import datetime
from dateutil.relativedelta import relativedelta
from datetime import date
six_months = date.today() + relativedelta(months=-6)
However I would like six_months to be in "%Y%m%d" format.
Thank you for your help.
You're looking for strftime.
six_months.strftime('%Y%m%d')
This should do the job for you.

Categories

Resources