Thanks for the response. I must have spent hours looking on-line for a method to treat datetime variables yet not one site mentioned the "pickle" module you indicatged. I did, however solve my problem. It may be a kluge but it seems to work.
I learned that I cannot use print() to display the value of datetime but once I saved it to a file, I could see it. If I used "d3 = d2.isoformat" it could be sent to a file with a write statement. Apparently, it gives a write/read format and places a T between the date and time as a separator. In trying to read it back into the program and work the calculation, I had to replace the T with a space and some formatting. It all worked. #=============================================== LBD = "LBD" d2 = datetime.now() d2i = d2.isoformat() with open("TimeDate.txt", 'r') as infile: for BottleInfo in infile: # loop to find each line in the file for that dose BottleInfo = BottleInfo.strip() if ((BottleInfo[0:3]== "LBD")): BottleData = BottleInfo[0:43].strip() BottleDataA = BottleData[4:14].strip() BottleDataB = BottleData[16:30].strip() BottleDataC = BottleDataA + " " + BottleDataB print("BottleDataC = <" + BottleDataC + ">") # I guess I could have searched for the "T" and replaced it. print() d1 = BottleDataC import datetime dto = datetime.datetime.strptime(d1, '%Y-%m-%d %H:%M:%S.%f') dti = dto.isoformat() HoursDiff = int((d2-dto).total_seconds()/3600) print("HoursDiff = " + str(HoursDiff)) print() TimeDateInfo=open("TimeDate.txt", "a") TimeDateInfo.write("{0:>5} {1:>25} {2:>5}\n".format (LBD, d2i, HoursDiff)) TimeDateInfo.close() # =========================================== Granted, there may be other ways to do this but I actually enjoy the exploration... Still, I would like to see other methods. Steve -----Original Message----- From: Dieter Maurer <die...@handshake.de> Sent: Sunday, October 11, 2020 12:48 PM To: Steve <Gronicus@SGA.Ninja> Subject: Re: Problem saving datetime to file and reading it back for a calculation Steve wrote at 2020-10-10 18:17 -0400: >I would like to use the line: >HoursDiff = int((d2-d1).total_seconds()/3600) to determine the >difference in hours between two timedate entries. > >The variable d2 is from datetime.now() >and d1 is read from a text file. > >I can save d2 to the file only if I convert it to string and, at a later >date, it gets read back in as d1 as string. The variable d1 as string will >not work in the HoursDiff statement. Python's "pickle" module provides support for storing (most) objects to files and read them back. -- https://mail.python.org/mailman/listinfo/python-list