I want to get the seconds that expired since last midnight. What's the most elegant way 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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With