This is what I have so far, how would I change to add any hours and minutes input by the user?
from datetime import datetime
str(datetime.now())[00:00]
addTime =
You can just use the addition operator + to add two timestamps, so to add 10 minutes to the current time and store it in a variable named addTime your code would look like this:
import datetime
addTime = datetime.datetime.now() + datetime.timedelta(minutes=10)
Related
I would like to add a certain time to a formatted time string in python. For this I tried the following
from datetime import datetime, timedelta
timestemp_Original = '2021-07-13T00:15:00Z'
timestemp_Added1 = '2021-07-13T00:15:00Z' + timedelta(minutes=15)
timestemp_Added2 = timestemp_Original + datetime.timedelta(hours=0, minutes=15)
but this leads to error messages (I took it from here How to add hours to current time in python and Add time to datetime). Can aynone tell me how to do this?
First, You need to convert str to datetime with a specific format of string_date and then use timedelta(minutes=15).
from datetime import datetime, timedelta
timestemp_Original = '2021-07-13T00:15:00Z'
timestemp_Added1 = datetime.strptime(timestemp_Original, "%Y-%m-%dT%H:%M:%SZ") + timedelta(minutes=15)
print(timestemp_Added1)
# If you want to get as original format
print(timestemp_Added1.strftime("%Y-%m-%dT%H:%M:%SZ"))
# 2021-07-13T00:30:00Z
2021-07-13 00:30:00
I want to add hours to a datetime and use:
date = date_object + datetime.timedelta(hours=6)
Now I want to add a time:
time='-7:00' (string) plus 4 hours.
I tried hours=time+4 but this doesn't work. I think I have to int the string like int(time) but this doesn't work either.
Better you parse your time like below and access datetime attributes for getting time components from the parsed datetime object
input_time = datetime.strptime(yourtimestring,'yourtimeformat')
input_seconds = input_time.second # for seconds
input_minutes = input_time.minute # for minutes
input_hours = input_time.hour # for hours
# Usage: input_time = datetime.strptime("07:00","%M:%S")
Rest you have datetime.timedelta method to compose the duration.
new_time = initial_datetime + datetime.timedelta(hours=input_hours,minutes=input_minutes,seconds=input_seconds)
See docs strptime
and datetime format
You need to convert to a datetime object in order to add timedelta to your current time, then return it back to just the time portion.
Using date.today() just uses the arbitrary current date and sets the time to the time you supply. This allows you to add over days and reset the clock to 00:00.
dt.time() prints out the result you were looking for.
from datetime import date, datetime, time, timedelta
dt = datetime.combine(date.today(), time(7, 00)) + timedelta(hours=4)
print dt.time()
Edit:
To get from a string time='7:00' to what you could split on the colon and then reference each.
this_time = this_time.split(':') # make it a list split at :
this_hour = this_time[0]
this_min = this_time[1]
Edit 2:
To put it all back together then:
from datetime import date, datetime, time, timedelta
this_time = '7:00'
this_time = this_time.split(':') # make it a list split at :
this_hour = int(this_time[0])
this_min = int(this_time[1])
dt = datetime.combine(date.today(), time(this_hour, this_min)) + timedelta(hours=4)
print dt.time()
If you already have a full date to use, as mentioned in the comments, you should convert it to a datetime using strptime. I think another answer walks through how to use it so I'm not going to put an example.
With the datetime module, I can get the current time, like so:
>>> datetime.now().strftime('%Y-%m-%d %H:%M:%S')
'2017-08-29 23:01:32'
I have access to the time at which a file was created, in the same format:
>>> data['created']
'2017-08-29 20:59:09'
Is there a way, using the datetime module, that I can calculate the time between the two, in hours?
Performing subtraction on two datetime objects will result in a timedelta. You can use datetime.strptime to get that second datetime object, access the seconds attribute of that timedelta and calculate the hours from there:
from datetime import datetime
...
file_created = datetime.strptime(data['created'], '%Y-%m-%d %H:%M:%S')
difference = (datetime.now() - file_created).seconds
print("Hours since creation: " + str(difference // 3600)) # 3600 seconds in 1 hour
How to get the current date with zero hours in python?
When i am trying datetime.datetime.now() it is showing current date & time but i would like to get current date with zero hours.
You can use strftime to format the time as follows, this will give you the 'time' without the hours, minutes etc:
timeFormat = "%Y-%m-%d"
time_now = time.strftime(time.time())
Alternatively, if you want todays date, without a time aspect, use this from datetime
from datetime import date
today = date.today().strftime("%Y-%m-%d_%H:%M")
You can try the following code if you don't want to show hours in your code:
print str(datetime.datetime.now().date()) + " "+str(datetime.datetime.now().minute) + ":" + str(datetime.datetime.now().second)
In the model there is a field DateTimeField(), the database stores the value of the form 2015-09-21 17:37:11. How to make a selection, for the last several hours. For example, in the sample were only the values for the last 3 hours.
You need to build the date reference manually, and use it in the query.
from datetime import datetime, timedelta
now = datetime.now()
before = now - timedelta(hours=3)
qs = MyModel.objects.filter(date__gte=before)
As a reference, the datetime module.
Note that if you use locale-aware times in your application (through USE_TZ=True setting), you will need to change the way you get current time to this:
from django.utils import timezone
now = timezone.now()
Try:
from datetime import datetime, timedelta
delta = datetime.now() - timedelta(hours=3)
Model.objects.filter(date_time__gte=delta)