2

Hello im utterly new to python, i have this code for showing current time:

from datetime import datetime
time = datetime.now()
print(time)

workes fine but output look like this:

2018-04-05 10:55:36.615329

how do i get rid of all those "milliseconds"? so output would look like this

2018-04-05 10:55:36
0

3 Answers 3

1

While strftime is probably the way to go when dealing with datetime, I decided to answer the general question of removing chars from a string after the dot.

Essentially, this is your string:

s = str(datetime.now())

This code would remove all chars after the .

dt = s.split('.',1)[0]
print (dt)
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

from datetime import datetime
time = datetime.now()
print(time.strftime('%Y-%m-%d %H:%M:%S'))

Comments

0

don't use time as a vairable, instead use any other name for that, you can achieve your output with the following

t = datetime.now()
t.strftime("%YY-%m-%d %H:%M:%S")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.