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
vikino
  • 109
  • 1
  • 7

3 Answers3

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)
Uri Goren
  • 13,386
  • 6
  • 58
  • 110
1

Try this:

from datetime import datetime
time = datetime.now()
print(time.strftime('%Y-%m-%d %H:%M:%S'))
Sr7
  • 86
  • 1
  • 9
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")
Gopi Krishna
  • 108
  • 2
  • 16