Mario
Mario

Reputation: 1966

How can pass the current datetime to string in python?

I'm trying to pass the current date and time of activities to string value so that whenever I saved my picture or anything during the program it can be saved by its own certain name + current time together but I faced error probably due to : ! Following format would fulfill my expectation :

2019-05-22 21-33-34 instead of 2019-05-22 21:33:34.433134 
import datetime as dt
import matplotlib.pyplot as plt

dtime = dt.time()
now=dt.datetime.now()
now.isoformat()
print(now)

...

plt.savefig(f'{now}.png')

...

test_RNN.to_csv(f'test_RNN_history{now}.csv', sep=',', header=None, index=None)

Upvotes: 0

Views: 350

Answers (2)

bejp
bejp

Reputation: 21

You can convert the timestamp to a string and then can use the replace() method to change the : to -

str(now).replace(":","-")

Upvotes: 2

mad_
mad_

Reputation: 8273

Use strftime

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

Upvotes: 3

Related Questions