0

I want to be able to get 10 digits after the period in the seconds. i want to be able to have something as follows:

14-07-2002 10:40:20.xxxxxxxxxx

how can i modify the below posted line of code to achieve that please?

code:

return datetime.now().strftime("%d-%b-%Y (%H:%M:%S.%f)")
Amrmsmb
  • 1
  • 27
  • 104
  • 226

1 Answers1

1

You can use time.time_ns()

import time
from datetime import datetime

nanos = time.time_ns()
secs = nanos / 1e9

dt = datetime.fromtimestamp(nanos / 1e9)
timeWithNS = '{}{:03.0f}'.format(dt.strftime('%Y-%m-%d %H:%M:%S.%f'), nanos % 1e3)
print(timeWithNS)
# 2022-07-14 11:30:26.059128064

Although this may be somewhat limited because your hardware / OS may not fully support nanoseconds

cupoftea
  • 69
  • 11
  • yes, but that will provide a value like a timepstame since the last epoch. i want to have 10 digits after the poriod in the seconds value – Amrmsmb Jul 14 '22 at 10:20
  • Just updated my answer - hopefully this answers your question more fully – cupoftea Jul 14 '22 at 10:31