falukky
falukky

Reputation: 1139

Python: How to ensure that my text datetime will converted to same format successfully

I have log text file and from each text I have datetime with this format:

2019-04-14 19:42:25.321279

And I also have current time:

time = datetime.datetime.now()

This current time will get from several machines and I want to unsure that this format will be the same as my log format.

What is the best option to do that ?

I was thinking maybe to read the current time:

time = datetime.datetime.now()

So this time could be with other format so maybe I could convert it to long and then to my log format?

Upvotes: 0

Views: 35

Answers (1)

J...S
J...S

Reputation: 5207

You could use the strftime() function of datetime.datetime object to format the data into string form.

Use

datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")

to get a string like

2019-04-14 16:35:07.750509

assuming the last number indicates microseconds.

See http://strftime.org/ for a list of format specifiers available.

Upvotes: 2

Related Questions