-3

i am new to python 2.7, wondering and facing issue how to convert datetime to timestamp epoch.

Also the date time is in current UTC. example : 2016-05-11 18:33:44 to 1462958071

moooeeeep
  • 31,622
  • 22
  • 98
  • 187
Vinoth Mohan
  • 35
  • 1
  • 7
  • @EdChum yes, flagging as such. – Joseph Farah May 18 '16 at 15:45
  • the example in the question is incorrect: `str(datetime.utcfromtimestamp(1462958071)) == '2016-05-11 09:14:31'`, not `2016-05-11 18:33:44` i.e., the utc time (`2016-05-11 18:33:44`) does not correspond to "timestamp epoch" (`1462958071`). The corresponding POSIX timestamp is `1462991624`. – jfs May 18 '16 at 20:51

1 Answers1

-1

Convert string to python datetime object and then parse the seconds

a ="2016-05-11 18:33:44"
b= datetime.strptime(a,"%Y-%d-%m %H:%M:%S")
print b.strftime('%s') # returns time in epoch
Tanu
  • 1,503
  • 12
  • 21
  • Awesome ,its working...! – Vinoth Mohan May 18 '16 at 15:23
  • @VinothMohan: the answer is *wrong* unless your local timezone is utc and you are on a platform that supports `%s` (*nix). There is no need for that: [there is a portable solution to convert a datetime that represents UTC time to "timestamp epoch"](http://stackoverflow.com/a/13423091/4279) – jfs May 18 '16 at 20:46