The api I am working with gives time is the following format when I place an order.
'orderDateTime': '12-May-2020 14:54:11'
What I am looking to do is to find the number of minutes/seconds that have passed since I placed the order. So if it has already been for example 10 minutes, if I would like to cancel or modify the order I can do it.
I have tried everything I know to convert the given time format to do what I want but have been unsuccessful. Please help. Thanks in advance.
time_now = datetime.now()
print("Time now is",time_now)
t1 = time_now.strftime("%d-%b-%Y %H:%M:%S")
print(t1)
trade_time = datetime(12-May-2020 15:01:32)
t2 = datetime.strftime(trade_time,"%d-%b-%Y %H:%M:%S")
print(t2)
You are mixing the datetime objects and their representation as strings.
What you need to do is convert your trade time to a datetime object, by using the strptime method. strftime does the opposite, it produces a formatted text representation of your datetime.
Then, you can subtract the two datetime objects, which will give you the difference as a timedelta, from which you can get the difference as a number of seconds.
So, your code should look like:
from datetime import datetime
time_now = datetime.now()
trade_time_as_str = '12-May-2020 15:01:32'
trade_time = datetime.strptime(trade_time_as_str,"%d-%b-%Y %H:%M:%S")
elapsed = time_now - trade_time
elapsed_seconds = elapsed.total_seconds()
print(elapsed_seconds)
# 15808.77104
Related
My code is the following:
date = datetime.datetime.now()- datetime.datetime.now()
print date
h, m , s = str(date).split(':')
When I print h the result is:
-1 day, 23
How do I get only the hour (the 23) from the substract using datetime?
Thanks.
If you subtract the current date from a past date, you would get a negative timedelta value.
You can get the seconds with td.seconds and corresponding hour value via just dividing by 3600.
from datetime import datetime
import time
date1 = datetime.now()
time.sleep(3)
date2 = datetime.now()
# timedelta object
td = date2 - date1
print(td.days, td.seconds // 3600, td.seconds)
# 0 0 3
You're not too far off but you should just ask your question as opposed to a question with a "real scenario" later as those are often two very different questions. That way you get an answer to your actual question.
All that said, rather than going through a lot of hoop-jumping with splitting the datetime object, assigning it to a variable which you then later use look for what you need in, it's better to just know what DateTime can do since that can be such a common part of your coding. You would also do well to look at timedelta (which is part of datetime) and if you use pandas, timestamp.
from datetime import datetime
date = datetime.now()
print(date)
print(date.hour)
I can get you the hour of datetime.datetime.now()
You could try indexing a list of a string of datetime.datetime.now():
print(list(str(datetime.datetime.now()))[11] + list(str(datetime.datetime.now()))[12])
Output (in my case when tested):
09
Hope I am of help!
I'm trying to convert strings into a date time object given the hours minutes seconds and milliseconds, so I can find the difference in time between the two strings. If there is a better way to do this please let me know, that'd be great.
the format of the date time object looks like this:
But when I tried subtracting two strings converted into date time objects the result wasn't correct. for example:
from datetime import datetime
format = '%H:%M:%S:%f'
x = '12:51:11:153'
time1 = datetime.strptime(x, format)
y = '13:51:11:153'
time2 = datetime.strptime(y, format)
difference = time2 - time1
print(difference)
"The print" = 0:00:00
Am I missing something for the format, or is there a special method to subtract datetime objects from each other? Also, is there an easier way to do this?
I think your solution is right. I can't reproduce that. This is my code and the output if I run it.
from datetime import datetime
import time
format = '%H:%M:%S:%f'
time1 = datetime.strptime("12:51:11:153", format)
time2 = datetime.strptime("13:51:11:153", format)
print((time2-time1).total_seconds())
--> 3600.0 seconds (type is float)
type(time2-time1) --> <class 'datetime.timedelta'>
time2-time1 --> datetime.timedelta(seconds=3600)
I want to add a time to a datetime. My initial datetime is: initial_datetime='2015-11-03 08:05:22' and is a string and this_hour and this_min are strings too. I use:
time='-7:00'
time = time.split(':')
this_hour = time[0]
this_min = time[1]
initial_datetime='2015-11-03 08:05:22'
new_date = datetime.combine(initial_datetime, time(this_hour, this_min))
+ timedelta(hours=4)
But there comes an error:
'str' object is not callable.
My desired output is the initial_datetime plus my time (in this case -7 hours ) and then add 4 hours. So, in my example, the new date should be '2015-11-03 05:05:22'.
datetime.combine is typically used to combine a date object with a time object rather than incrementing or decrementing a datetime object. In your case, you need to convert your datetime string to a datetime object and convert the parts of your time string to integers so you can add them to your datetime with timedelta. As an aside, be careful about using variable names, like time, that conflict with your imports.
from datetime import datetime, timedelta
dtstr = '2015-11-03 08:05:22'
tstr = '-7:00'
hours, minutes = [int(t) for t in tstr.split(':')]
dt = datetime.strptime(dtstr, '%Y-%m-%d %H:%M:%S') + timedelta(hours=hours+4, minutes=minutes)
print(dt)
# 2015-11-03 05:05:22
I do not even know how to tackle this. I need to first turn a str() into a datetime object, convert it to epoch time add a number of seconds then turn it back into the date in a properly formatted object. A sample of the str is:
"2016-11-04T03:02:00Z"
I'm guessing some regex to break up the str()??
Use a timedelta object, e.g:
from datetime import datetime, timedelta
TIMESTRING_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
dt = datetime.strptime('2016-11-04T03:02:00Z', TIMESTRING_FORMAT)
ndt = dt + timedelta(seconds=5)
print datetime.strftime(ndt, TIMESTRING_FORMAT)
See docs https://docs.python.org/2/library/datetime.html and make sure that my TIMESTRING_FORMAT string is correct.
Obviously I can get the date and time from datetime.datetime.now(), but I don't actually care about the seconds or especially microseconds.
Is there somewhere I can easily get Date+Hour+Minute?
You can clear down the second and microsecond component of a datetime value like so:
dt = datetime.datetime.now()
#Now get rid of seconds and microseconds component:
dt = dt.replace(second=0, microsecond=0)
This would allow you to compare datetimes to minute granularity.
If you just want to print the date without a second/microsecond component, then use the appropriate format string:
dt = datetime.datetime.now()
print dt.strftime("%Y/%m/%d %H:%M")
>>> '2012/12/12 12:12'