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
Related
This question already has answers here:
How do I turn a python datetime into a string, with readable format date?
(8 answers)
Closed 1 year ago.
current_time = datetime.datetime.now()
print("current time ", current_time)
Result:
current time 2021-03-08 23:22:59.912410
Here, I want only up to minutes(2021-03-08 23:22) and need to get rid of seconds and milliseconds from the current time.
Please help
You can use strftime() to output dates on a specific format, i.e.:
import datetime
current_time = datetime.datetime.now()
print("current time ", current_time.strftime("%Y-%m-%d %H:%M:%S"))
# current time 2021-03-09 04:42:57
Demo
You have to re-format the time in string as per the format you want.
formatted = current_time.strftime("%Y-%m-%d %H:%M:%S")
print(formatted)
Output will be like:
'2021-03-09 10:12:58'
It also changes the datetime object to str. And hence datetime operations can not be performed on it. It is recommended to use only for output presentation.
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)
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)
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"))
This question already has answers here:
Parsing datetime strings containing nanoseconds
(5 answers)
Closed 4 years ago.
I have a date formated like this: "2018-06-12T13:58:36.663550655Z"
I want be convert this string into a Unix time stamp.
This is my code:
from datetime import datetime
date = '2018-06-12T14:03:35.306662173Z'
time = datetime.strptime(date,"%Y-%m-%dT%H:%M:%S.%fZ")
unixTime = datetime.utcfromtimestamp(time)
When i run it, there's a error:
ValueError: time data '2018-06-12T14:03:35.306662173Z' does not match
format '%Y-%m-%dT%H:%M:%S.%fZ'
Would be nice if someone could help me !
Thanks!
Try stripping the extra info.
Ex:
import time
from datetime import datetime
date = '2018-06-12T14:03:35.306662173Z'
d = datetime.strptime(date[:26],"%Y-%m-%dT%H:%M:%S.%f")
unixTime = time.mktime(d.timetuple())
print(unixTime)
Output:
1528792415.0