Reputation: 145
I want to convert this '2018-03-15T05:13:24.000-0400' datetime format to the epoch time format. While converting I am getting the time format error. Plz help me with this issue.
Here is my python Code:
import time
start='2018-03-15T05:13:24.000-0400'
start1 = str(start)
start_date = int(time.mktime(time.strptime(start1,'%Y-%m-%d %H:%M:%S')))*1000
print(start_date)
Expecting output:
1521105204000
Thanks :)
Upvotes: 1
Views: 277
Reputation: 365915
First, your strptime
format has to actually match the string you pass it. Let's break it down:
2018
, 4-digit year: %Y
-
, literal hyphen: -
03
, 2-digit month: %m
-
, literal hyphen: -
15
, 2-digit day: %d
T
, ISO-style date/time separator: T
(not a space as in your format)05
, 2-digit hour: %H
:
, literal colon: :
13
, 2-digit minute: %M
:
, literal colon: :
24
, 2-digit second: %S
.
, literal dot: .
000
, 3-digit millisecond, which you can parse as a 3-digit microsecond and fix later: %f
-0400
, 5-char UTC offset, %z
Next, since for some reason using the time
module instead of datetime
, and using the struct_tm
type which doesn't support sub-second times, and then truncating to int
anyway, even though you apparently want milliseconds, that %f
microsecond value is going to end up ignored, so you don't even have to fix it up.
So just put this string together, and you're done.
Upvotes: 2