seanspotatobusiness
Posts: 151
Joined: Tue May 22, 2012 11:19 pm

datetime without the microseconds?

Fri May 18, 2018 12:27 am

I have a script which was partly written by a friend and it includes the following line to print the time at the beginning of each line. My problem is that the time looks like this: 2018-05-18 01:19:04.934426 and I'd rather it looked like this: 2018-05-18 01:19 without the microseconds. Can anyone advise if the line below can be modified a bit to get rid of the unwanted part? Thanks

Code: Select all

def log(line):
    print str(datetime.datetime.now()) + "\t" + line
Raspberry Pi 3 B and B+
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"

User avatar
Paul Webster
Posts: 747
Joined: Sat Jul 30, 2011 4:49 am
Location: London, UK

Re: datetime without the microseconds?

Fri May 18, 2018 6:01 am

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

seanspotatobusiness
Posts: 151
Joined: Tue May 22, 2012 11:19 pm

Re: datetime without the microseconds?

Fri May 18, 2018 9:39 am

Thanks, Paul; that worked!
Raspberry Pi 3 B and B+
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"

Return to “Python”

Who is online

Users browsing this forum: Google [Bot] and 10 guests