Comparing date in python and applying a condition on comparison [duplicate] - python

This question already has answers here:
How to compare two dates?
(6 answers)
Closed 3 years ago.
i want to compare/subtract two date's due date from current date in python and apply a condition on this subtraction that if the difference is >0 days then calculate fine by multiplying difference with fine per day
from datetime import date
a=date.today()
issuedate=date(2019,5,9)
duedate#should be 5 days after issue date i can't find the method for doing this
check=a-duedate
# if check>0days:
# print(check days*40)

You need to use the timedelta function in the datetime module:
from datetime import date
import datetime
a=date.today()
issuedate=date(2019,5,9)
duedate = issuedate+datetime.timedelta(days=5)
check=a-duedate
print(check>=datetime.timedelta(days=0))

Use timedelta
from datetime import timedelta
duedate = issuedate + timedelta(days=5)
check=(a-duedate).days

Related

pandas calculate time difference to now [duplicate]

This question already has answers here:
Compare timestamp with datetime
(2 answers)
Timestamp String in Zulu Format To Datetime
(1 answer)
Closed 9 months ago.
I have a pandas dataframe df with a time column containing datetime values. I now want to filter the dataframe to show rows with time values lying in the next 15 minutes.
So first I try to simply subtract the current time from the datetimes.
df.Time = pd.to_datetime(df.Time)
print(df.Time - pd.to_datetime("today"))
But got this error:
TypeError: Cannot subtract tz-naive and tz-aware datetime-like objects
I tried to remove the tz-awareness with .replace(tzinfo=None) but it was not working. In the end I am looking for a command like this (assuming the difference of two datetimes in is minutes):
df.loc[df.Time - pd.to_datetime("today") < 15]

Python: Add one day to custom date? [duplicate]

This question already has answers here:
Add 1 day to my date in Python [duplicate]
(1 answer)
Adding days to a date in Python
(16 answers)
Closed 2 years ago.
I'm trying to add one day to custom date(in string). Time format is dd/mm/yyyy
Sample input:
'02/11/2020'
Output:
'03/11/2020'
from datetime import datetime, timedelta
date = '02/11/2020'
old = datetime.strptime(date, "%d/%m/%Y")
new = old + timedelta(days=1)
new_date = new.strftime("%d/%m/%Y")
print(new_date) # '03/11/2020'
You can use the datetime module
from datetime import datetime, timedelta
start = datetime.strptime("%d/%m/%Y", "02/11/2020")
end = start + datetime.timedelta(days = 1)

How to subtract days from a date using datetime [duplicate]

This question already has answers here:
How to perform arithmetic operation on a date in Python?
(2 answers)
Closed 2 years ago.
Something surely extremely simple, but I've been browsing around for almost one hour and couldn't find:
Working with Python, I have a date d="2020-01-22" (means January, 22nd, 2020) and I want to calculate the date corresponding to d - 57 days. With datetime, surely, but how, exactly?
Use the following code-
from datetime import datetime, timedelta
d = datetime.today()
new_d = d - timedelta(days=57)
Use package datetime.
# python3
import datetime
d = datetime.datetime.strptime("2020-01-22", '%Y-%m-%d')
print(d - datetime.timedelta(days=57)) # 2019-11-26 00:00:00
You can use datetime.strptime. this is the main function for parsing strings into datetimes. It can handle all type of formats, with the format determined by a format string you provide. You can read in detail here
from datetime import datetime
date_time= datetime.strptime('2020-01-22", '%Y-%m-%d')
print(date_time)

Find monday of current week in Python [duplicate]

This question already has answers here:
Getting the date of the first day of the week
(2 answers)
Python: give start and end of week data from a given date
(5 answers)
Closed 3 years ago.
I am trying to get the timestamp of monday at 00:00 of the current week in python. I know that for a specific date, the timestamp can be found using
baseTime = int(datetime.datetime.timestamp(datetime.datetime(2020,1,1)))
However, I want my program to automatically find out, based on the date, which date monday of the current week was, and then get the timestamp. That is to say, it would return different dates this week and next week, meaning different timestamps.
I know that the current date can be found using
import datetime
today = datetime.date.today()
Thanks in advance
I am trying to get the timestamp of monday at 00:00 of the current week in python
You could use timedelta method from datetime package.
from datetime import datetime, timedelta
now = datetime.now()
monday = now - timedelta(days = now.weekday())
print(monday)
Output
2020-01-27 08:47:01

Get the date name from a datetime object [duplicate]

This question already has answers here:
How to get day name from datetime
(6 answers)
Closed 3 years ago.
Is it possible to get the name of a day (e.g. Monday) from a datetime object?
In my pandas dataframe I have a column of type datetime e.g.
2016-07-01
and so on
I want to create a new column with the name of that day
Thanks in advance
duplicate of this?
How to get day name in datetime in python?
import datetime
now = datetime.datetime.now()
print(now.strftime("%A"))
or:
>>> from datetime import datetime as date
>>> date.today().strftime("%A")
'Monday'

Categories