0

How to convert a date Object to millisecond in python 2.6.6

 date_dob = getattr(model, value)
                 a = date_dob.timestamp() * 1000
                 print(a)

Here date_dob is something like "1993-05-29 13:42:10.298Z"

I am getting below error

AttributeError: 'datetime.datetime' object has no attribute 'timestamp'

I am expecting result of 738639730000(epoch)

ANP
  • 945
  • 8
  • 20
  • Milliseconds are a unit of an *interval* of time; a date represents an *instant* of time. You can't convert an instant into an interval unless you have another instant that you are *relative* to, called the "epoch". Given your numbers, it sounds like you're working with an epoch of approximately 1969-12-31 00:00:00 UTC? That's an unusual epoch: the Unix epoch of 1970-01-01 00:00:00 UTC is much more common. – Daniel Pryden Mar 15 '19 at 14:54

1 Answers1

0

timestamp() was introduced in Python 3.

Use the below code for python 2.

from datetime import datetime

date_dob = datetime.now()

print date_dob

#print dir(date_dob) ## To list all the attributes

date_sec_str = date_dob.strftime("%s.%f")
date_sec_float = float(date_sec_str)

a = int(date_sec_float*1000)

print a

Result:

2019-03-15 15:32:05.869709
1552663925869

HariUserX
  • 1,263
  • 1
  • 7
  • 16