Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
xxxxxxx to date/time
#1
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.
Reply
#2
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?
Reply
#3
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:
Reply
#4
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.
Reply
#5
Could you clarify what exactly is the output you want? You can use strftime to go back to a timestamp.
Reply
#6
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.
Reply
#7
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.
Reply
#8
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:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 214 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Date Time Series Help...Please spra8560 2 352 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Python date format changes to date & time 1418 4 587 Jan-20-2024, 04:45 AM
Last Post: 1418
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 1,017 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  Formatting a date time string read from a csv file DosAtPython 5 1,253 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  Wait til a date and time KatManDEW 2 1,418 Mar-11-2022, 08:05 PM
Last Post: KatManDEW
  Date format and past date check function Turtle 5 4,233 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,225 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  Naming the file as time and date. BettyTurnips 3 2,970 Jan-15-2021, 07:52 AM
Last Post: BettyTurnips
  Update Date based on Time/String stevezemlicka 1 2,025 Jan-08-2021, 06:54 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020