Posts: 63
Threads: 46
Joined: Nov 2017
Hi folks.
I'm just getting used to Python, I've been looking through the strp/strf date functions, but I can't find exactly what I need.
I have a database field which is YYYYMMDDHHMM, it's a string, it's legacy, I can't change it. I now have a need to extract that date, calculate how many hours from 'then' to now, then write a calculated value back to the field in the same format as it came out.
I've got the string out, and I can pull it apart into bits as strings, but I can't see a strpdate example that fits. I'll be ok calculating the difference and writing the data back, but could someone please explain how I can get that original string into a ddatetime object?
Many thanks.
Posts: 2,342
Threads: 62
Joined: Sep 2016
I Google'd "python time format" and foundĀ http://strftime.org/. Using that, I constructed the format stringĀ "%Y%m%d%H%M" by just searching through the page.
Does that work for your use-case?
Posts: 5,151
Threads: 396
Joined: Sep 2016
Dec-01-2017, 04:53 PM
(This post was last modified: Dec-01-2017, 05:04 PM by metulburr.)
datetime is easy to calculate difference between two datetime objects
>>> import datetime
>>> d = '198702270830'
>>> then = datetime.datetime.strptime(d, '%Y%m%d%I%M')
>>> now = datetime.datetime.now()
>>> then
datetime.datetime(1987, 2, 27, 8, 30)
>>> now
datetime.datetime(2017, 12, 1, 11, 42, 28, 190791)
>>> duration = now - then
>>> duration
datetime.timedelta(11235, 11548, 190791)
>>> duration.total_seconds()
970715548.190791
>>> duration.days
11235
>>> divmod(duration.total_seconds(),31556926) #years
(30.0, 24007768.19079101)
>>> divmod(duration.total_seconds(),86400) #days
(11235.0, 11548.190791010857)
>>> divmod(duration.total_seconds(),3600) #hours
(269643.0, 748.1907910108566)
>>> divmod(duration.total_seconds(),60) #minutes
(16178592.0, 28.19079101085663) more info here
https://pymotw.com/2/datetime/#timedeltas
Recommended Tutorials:
Posts: 63
Threads: 46
Joined: Nov 2017
I've made a little headway, I think I've managed to get the date into a variable, but now I have an array problem I wasn't expecting.
if I say:
now=datetime.now()
then=[my_strptime]
print now, then
In this case 'now' gives me a readable date, then gives me an array of the date and time. It's all correct, but I want to be able to print it, or work with it making sure I'm not trying to calculate with a carrot and a parsnip. I'd sooner be working with 2 carrots.
Posts: 2,342
Threads: 62
Joined: Sep 2016
Could you clarify what exactly is the output you want? You can use strftime to go back to a timestamp.
Posts: 63
Threads: 46
Joined: Nov 2017
Dec-01-2017, 04:57 PM
(This post was last modified: Dec-01-2017, 05:47 PM by MuntyScruntfundle.)
Ahhhh, thank you. :o)
I was on the right track, just getting confused. Python is an odd syntax when you're used to decades of only one other language. I'm not sure yet what I should be searching for to find answers. So thanks again.
Ok, so I'm at the final hurdle and stumbling badly!!
I now have, as a datetime, which is the difference between then and now, the output:
31 days, 17:48:57.082677
Which is very nice and definitely accurate. However... I need the total hours. As this is a datetime, how do I calculate with it? What do I need to get xxx hours, preferably as an integer.
Thanks.
As per first post, I need the total hours between the dates so I can make a load of calculations, depending on these calculations I write the date and time back to the record. Im ok with the formatting of the strings now, I think, I'm just not sure haw to get the total hours between the two times.
Posts: 63
Threads: 46
Joined: Nov 2017
I've battled my way through it, and while I haven't got it perfect yet, I can say I'm at a point where I can live with it for now.
Thanks for your input folks.
Posts: 5,151
Threads: 396
Joined: Sep 2016
Quote:Which is very nice and definitely accurate. However... I need the total hours. As this is a datetime, how do I calculate with it? What do I need to get xxx hours, preferably as an integer.
Yes sorry, i updated my original post with that after posting. Must of been after you saw that.
Recommended Tutorials:
|