This question already has answers here:
How to add hours to current time in python
(4 answers)
Closed 3 years ago.
I want to get current time and add it with an integer of hours. Example now is 11.00pm, May 12, 2019. I want to add 3 hours more. So the result would be 2.00 am May 13, 2019. Please help me to datetime + hours(integer type)
import datetime
currentDT = datetime.datetime.now()
print('Now is: '+ str(currentDT))
hours = int(input()) #any hours you want
result = currentDT + hours #it will get the errors here
Use datetime.now to obtain the current time, and add a datetime.timedelta:
from datetime import datetime, timedelta
n_hours = 3
date = datetime.now() + timedelta(hours=n_hours)
print(datetime.now())
# 2019-05-12 19:16:51.651376
print(date)
# 2019-05-12 22:16:51.464890
Related
This question already has answers here:
How to compare times of the day?
(8 answers)
Closed 3 years ago.
I'm trying to compare two time in python:
from datetime import time, datetime
start = datetime(2020, 1 , 18, 9,30)
current_time = datetime.now()
if (current_time == start):
print("Time Matched")
Now when i'm running the code and when time reaches above mentioned start time, it does not prints "Time is matched" Kindly help me out in what im trying to do with this code is that when start time matches my laptop time i'm trying to execute some code , buy i'm not able to do so.
**Your PC Note down time with millisecond that's why you are not able to get desired result **
If You Provide second in datetime obj then -\
from datetime import time, datetime
start = datetime(2020, 1 , 18, 9,30)
current_time = datetime.now()
start_str=str(start)
current_str=str(surrent_time)
if(start_str==current_str):
print(match)
**General Method **
if we want to match particular part This is 23 hour format if you want 12 hour format you can find diff parameter answer
start = datetime(2020, 1 , 18, 9,30)
current = datetime.now()
start_year=start.strftime("%Y")
start_month=start.strftime("%m")
start_day=start.strftime("%H")
start_hours=start.strftime("%H")
start_min=start.strftime("%M")
print(start_year,start_month,start_day,start_hours,start_min)
current_year=current.strftime("%Y")
current_month=current.strftime("%m")
current_day=current.strftime("%H")
current_hours=current.strftime("%H")
current_min=current.strftime("%M")
print(current_year,current_month,current_day,current_hours,current_min)
if((start_year==current_year) and (start_month==current_month) and (start_day==current_day) and (start_hours==current_hours) and (start_min==current_min)):
print("match")
This question already has answers here:
Generating dates to the days of a week in Python?
(3 answers)
Python - Add days to an existing date
(1 answer)
Adding days to a date in Python
(16 answers)
Closed 3 years ago.
I have a code that can tell the weather in entered location.
I want to make an option to print the weather for the next 3 days ,I need to send to my function 3 dates (with a loop), every time different date, how can I send dates of next 3 days from current day?
#This is my function
def weather(city, date):
#This is the part where I send it from the main to the function:
city = 'Paris'
while(i < 4):
i += 1
weather(city.lower(), dd/mm/yyyy)# Here instead of "dd/mm/yyyy" I need to send every time the next date from today.
Use datetime.timedelta(days=1) to increment your day by 1 as follows, you can pass this date to your function
import datetime
curr_date = datetime.datetime.now()
for i in range(4):
curr_date += datetime.timedelta(days=1)
print(curr_date)
#2019-04-19 22:01:29.503352
#2019-04-20 22:01:29.503352
#2019-04-21 22:01:29.503352
#2019-04-22 22:01:29.503352
The simplest way to generate a date range is to use pandas.date_range as
import pandas as pd
dates = pd.date_range('2019-04-10', periods=3, freq='D')
for day in dates:
weather(city, day)
Or you may insist on a loop for next days, you can use datetime.timedelta
from datetime import date, timedelta
one_day_delta = timedelta(1)
day = datetime.date(2019, 4, 19)
for i in range(3)
day += one_day_delta
weather(city, day)
This question already has answers here:
Days between two dates? [duplicate]
(4 answers)
Closed 5 years ago.
I need to design a code that will automatically generate the date X number of days from current date.
For that, I currently have a function that returns an epoch timestamp, which I then add the fixed number of days in seconds. However, I am currently stuck there and do not know how to convert the epoch timestamp into a Gregorian calendar date format (DD/MM/YYYY HH:MM). Displaying time is optional, can be rounded to the nearest day.
A way to do this without using an epoch timestamp and directly getting the current date in a readable format, printing it, and adding X days to it before generating the second date, is also fine, but I have no idea how that would work.
Any help/input would be much appreciated. Thanks in advance.
You can use timedelta.
import datetime as dt
x = 5 # Days offset.
now = dt.datetime.now()
>>> now + dt.timedelta(days=x)
datetime.datetime(2017, 12, 2, 21, 10, 19, 290884)
Or just using days:
today = dt.date.today()
>>> today + dt.timedelta(days=x)
datetime.date(2017, 12, 2)
Easy enough to convert back to a string using strftime:
>>> (today + dt.timedelta(days=x)).strftime('%Y-%m-%d')
'2017-12-02'
import datetime
x = 5
'''
Date_Time = datetime.datetime.now()#It wil give Date & time both
Time = datetime.datetime.now().time()#It will give Time Only
'''
Date = datetime.datetime.now().date()#It will give date only
print(Date + datetime.timedelta(days=x))#It will add days to current date
Output:
2017-12-03
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
This question already has answers here:
What is the standard way to add N seconds to datetime.time in Python?
(11 answers)
Closed 6 years ago.
How can i add datetime.time objects to each other? Lets say i have:
import datetime as dt
a = dt.time(hour=18, minute=15)
b = dt.time(hour=0, minute=15)
#c = a+b???
c should be equal to datetime.time(hour=18, minute=30)
Edit:
I have a function that gets as arguments datetime.time objects and should return datetime.time object that is sum of passed arguments. As i am only dealing with hours and minutes i wrote this:
def add_times(t1, t2):
hours = t1.hour + t2.hour
minutes = t1.minute + t2.minute
hours += minutes // 60
minutes %= 60
new_time = datetime.time(hour=hours, minute=minutes)
return new_time
But it is a dirty way and i am sure there is a legit way of doing it.
How do i achieve that?
Adding timedeltas
You can add dt.timedeltas
import datetime as dt
a = dt.timedelta(hours=18, minutes=15)
b = dt.timedelta(hours=0, minutes=15)
a + b
datetime.timedelta(0, 66600)