7

I want to convert current datetime into Unix time stamp

My Code

import time
import datetime

d = datetime.datetime.now()
unixtime = time.mktime(d.timetuple())

print(unixtime)

My output:

1577098747.0

Expected Output:

1577098747123.0

Above code gives me timestamp upto 10 digits but I want it to be accurate till 13 digits.

Note: I don't want to convert it manually multiplying by 10**3 I want to capture accurate milliseconds.

Sociopath
  • 12,395
  • 17
  • 43
  • 69
  • 1
    you can use time.time() function – Sanchit.Jain Dec 23 '19 at 11:09
  • Your requirement is incorrect. What you have *is* a Unix timestamp (seconds since epoch); you seem to instead want milliseconds since the epoch. Neither of these have a specific number of digits, though obviously times around the current will have the expected number of digits. – tripleee Dec 23 '19 at 12:17

1 Answers1

5

do it like this

import time
import datetime

d = datetime.datetime.now()
unixtime = datetime.datetime.timestamp(d)*1000

print(unixtime)

or you just use time.time()

Sociopath
  • 12,395
  • 17
  • 43
  • 69
man zet
  • 750
  • 5
  • 22