git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Problem saving datetime to file and reading it back for a calculation


On 2020-10-11 20:25, Steve wrote:
> 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.

[snip]

Given:

      import datetime

To convert a datetime d to an ISO-format string, you can use:
     s = d.isoformat()

To convert an ISO-format string s to a datetime, you can use:

     d = datetime.datetime.fromisoformat()