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

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)

Related

Convert YYYY-MM-DD to DD-MMM-YYYY in Python [duplicate]

This question already has answers here:
Parse date string and change format
(10 answers)
Closed 24 days ago.
I'm trying to convert a list of dates (strings) in the format 2023-01-19 into the format 19-Jan-2023. The code I currently have does not work:
date_list = ['2023-01-19', '2023-01-07', '2022-11-29']
new_date_list = []
for date in date_list:
date_new_format = datetime.datetime(date, '%dd-%mmm-%yyyy')
new_date_list.append(date_new_format)
You have to first create a datetime object with strptime, then you can use strftime to reformat it:
from datetime import datetime
date_list = ['2023-01-19', '2023-01-07', '2022-11-29']
for date in date_list:
d = datetime.strptime(date, "%Y-%m-%d")
date_new_format = datetime.strftime(d, '%d-%b-%Y')
print(date_new_format)

How can I get the difference in days between today and a previous date? [duplicate]

This question already has answers here:
How to calculate number of days between two given dates
(15 answers)
Closed 2 years ago.
I'm trying to get the difference (in days) between today and a previous date here:
from datetime import date
today = date.today() # get today's date
print("Today's date:", today)
new_today = today.strftime("%Y, %#m, %Y") # convert it so that delta.days understands
print(new_today)
f_date = date(new_today)
l_date = date(2014, 7, 11)
delta = f_date - l_date
print(delta.days)
But I get an error:
TypeError: an integer is required (got type str)
I've read multiple threads on this, but they don't address using today's date in the calculation.
What's the best way to perform this calculation?
using datetime
import datetime
x = datetime.date.today()
y = datetime.date(2014, 7, 11)
diff = x-y
print(diff.days)
output
2392

How to format datetime object to date [duplicate]

This question already has answers here:
How do I convert a datetime to date?
(10 answers)
Closed 3 years ago.
I have a datetime object
v = 21.01.2019 14:25:37
I want to convert above datetime object to date as this
a = convert(v)
a = 21.01.2019
how i can do it
If you want today's date.try this :
datetime.datetime.now().date()
If you want your datetime object date :
v.date()

python get tommorows date dd.mm.yy problem [duplicate]

This question already has answers here:
How to increment a datetime by one day?
(8 answers)
Closed 4 years ago.
So basically I want to check if a certain string includes tommorows date so i made this date variable (see code).
Now the problem is every month on the last day this is going to be wrong. For example the 30th of september, with the way i did it, its going to say that tommorow will be the 31 of september, wich does not exist however. I need it to say that tommorow is the 1. of october.
Any suggestions? It has to be in the format dd.mm.yy please.
day = str(datetime.datetime.today().day+1)
month = str(datetime.datetime.today().month)
year = str(datetime.datetime.today().year)
date = day + "." + month + "." + year
just add one day to today
tomorrow = datetime.datetime.today() + datetime.timedelta(days=1)
print(str(tomorrow),tomorrow.strftime("%d.%m.%y"))
relativedelta can also be used
import datetime
from dateutil.relativedelta import relativedelta
tomorrow = datetime.datetime.today() + relativedelta(days=1)
print(str(tomorrow),tomorrow.strftime("%d.%m.%y"))

How to add hours to a timestamp [duplicate]

This question already has an answer here:
Python datetime add
(1 answer)
Closed 4 years ago.
I have a string variable some_time = "12:30", and I want to add 2 hours to it so the result is "14:30".
I believe by first turning the string into a timeformat,
temp_time = datetime.datetime.strptime(thetime, '%H:%M').time()
so that
>>>print (temp_time)
12:30:00
And then use Pytz can solve it. But I can't seem to find a Pytz command that deals with hh:mm alone, without yy:dd:mm
Any solutions?
If you use a timedelta, you can add two hours like:
Code:
def add_two_hours(time_string):
the_time = dt.datetime.strptime(time_string, '%H:%M')
new_time = the_time + dt.timedelta(hours=2)
return new_time.strftime('%H:%M')
Test Code:
import datetime as dt
print(add_two_hours('12:30'))
print(add_two_hours('23:30'))
Results:
14:30
01:30

Categories