Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get seconds since midnight in Python [closed]

I want to get the seconds that expired since last midnight. What's the most elegant way in Python?

like image 524
linqu Avatar asked Apr 12 '13 12:04

linqu


People also ask

How do you calculate seconds in Python?

Get Current Time in PythonUse the time. time() function to get the current time in seconds since the epoch as a floating-point number. This method returns the current timestamp in a floating-point number that represents the number of seconds since Jan 1, 1970, 00:00:00. It returns the current time in seconds.


1 Answers

It is better to make a single call to a function that returns the current date/time:

from datetime import datetime  now = datetime.now() seconds_since_midnight = (now - now.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds() 

Or does

datetime.now() - datetime.now() 

return zero timedelta for anyone here?

like image 78
eumiro Avatar answered Sep 20 '22 23:09

eumiro