Creating time stamps

Michael F. Stemper michael.stemper at gmail.com
Mon Jul 22 16:01:29 EDT 2019


I have some code that generates a time-stamp as follows:

  from datetime import datetime
  tt = datetime.now()
  timestamp = "%4d-%02d-%02d %02d:%02d" % \
    (tt.year, tt.month, tt.day, tt.hour, tt.minute)

I later realized that I could have written it as:

  from datetime import datetime
  from time import strftime
  timestamp = datetime.now().strftime( "%Y-%m-%d %H:%M" )

The first seems a little clunky with its accessing of multiple
attributes, but the second has an additional import. Is there
any reason to prefer one over the other?

-- 
Michael F. Stemper
There's no "me" in "team". There's no "us" in "team", either.



More information about the Python-list mailing list