I used : utctime = datetime.datetime(1601,1,1) + datetime.timedelta(microseconds = tup[5]) last_visit_time = "Last visit time:"+ utctime.strftime('%Y-%m-%d %H:%M:%S')
But I have the time of 1601, so the error show: ValueError: year=1601 is before 1900; the datetime strftime() methods require year >= 1900
I used python2.7, how can I make it? Thanks a lot!
The strftime() function is used to convert date and time objects to their string representation. It takes one or more input of formatted code and returns the string representation. Returns : It returns the string representation of the date or time object.
The strftime is obsolete and DateTime::format() provide a quick replacement and IntlDateFormatter::format() provied a more sophisticated slution.
The strftime() method returns a string representing date and time using date, time or datetime object.
strptime is short for "parse time" where strftime is for "formatting time". That is, strptime is the opposite of strftime though they use, conveniently, the same formatting specification.
You can do the following:
>>> utctime.isoformat() '1601-01-01T00:00:00.000050'
Now if you want to have exactly the same format as above:
iso = utctime.isoformat() tokens = iso.strip().split("T") last_visit_time = "Last visit time: %s %s" % (tokens[0], tokens[1].strip().split(".")[0])
Not that there seems to be a patch for strftime
to fix this behavior here (not tested)
the isoformat
method accepts a parameter specifing the character(s) dividing the date part from the time part of a datetime obj in its representation. Therefore:
>>> utctime.isoformat(" ") '1601-01-01 00:00:00.000050'
should do it. Furthermore, if you want to remove the microseconds you can operate a split.
>>> utctime.isoformat(" ").split(".")[0] '1601-01-01 00:00:00'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With