0

It seems that:

time.strftime("%Y-%m-%d %H:%M:%S")
#2017-01-12 10:46:57

datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
#2017-01-12 10:46:57

are equivalent. When I read many questions/answers here, or tutorials or documentation, some of them use the former, some the latter.

Are they 100% equivalent? When to prefer the one or the other?

PS: the latter seems to be equivalent to datetime.date.fromtimestamp(time.time()), but is this equivalent to time.strftime(...)?

Basj
  • 41,386
  • 99
  • 383
  • 673

1 Answers1

1

The former seems faster? and more here

import time
import datetime
%timeit time.strftime("%Y-%m-%d %H:%M:%S")
%timeit datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")

Output:

100000 loops, best of 3: 4.02 µs per loop
100000 loops, best of 3: 10.8 µs per loop
Community
  • 1
  • 1
Mohammad Yusuf
  • 16,554
  • 10
  • 50
  • 78