0

Is there any way to get the current time in microseconds resolution? specifically %y-%m-%dT%H:%M:%S:%f format?

Thanks!

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
pa1
  • 778
  • 3
  • 11
  • 26
  • https://stackoverflow.com/questions/58410614/how-to-get-the-millisecond-part-while-converting-to-date-time-from-epoch-using-p/58411334#58411334 – Rushikesh Raut Oct 17 '19 at 04:55

2 Answers2

2

You can print a datetime in that format just by using strftime:

>>> import datetime

>>> now = datetime.datetime.now()

>>> now
datetime.datetime(2019, 10, 17, 12, 45, 58, 294795)

>>> now.strftime("%Y-%m-%dT%H:%M:%S.%f")
'2019-10-17T12:45:58.294795'

Note that this is a separate issue to the resolution of your clock, it may be less than a microsecond. You can check that as well by using:

>>> str(datetime.time.resolution)
'0:00:00.000001'

As per the output, my Linux box (and Python 3.7 on my Win10 box) has a one-microsecond resolution.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • awesome. I know it answers the above question but can i add an additional query to this? How do i get difference between those formats? – pa1 Oct 17 '19 at 06:13
  • @Coder, not sure what you mean by difference between the formats. Do you mean, given two strings of the `'yyyy-mm-ddThh:mm:ss.ffffff'` form, what's the difference between them? If so, I'd just ask another question since it's only related tangentially to this one. There's no limit to the number of questions you can ask here :-) – paxdiablo Oct 17 '19 at 12:06
0

You should import datetime and then, use strftime to format date and time.

Import datetime

Now= datetime.now()
Print(now.strftime("%Y-%M-%D %H:%M:%S.%f"))

Comlete the parenthesis with your desired format.

Reyhaneh Torab
  • 360
  • 5
  • 9