从python的午夜开始秒 [英] Get seconds since midnight in python

查看:122
本文介绍了从python的午夜开始秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得自上个午夜以来的秒数。 python中最优雅的方式是什么?

解决方案

最好是对一个返回当前日期/时间:

  from datetime import datetime 

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

  datetime.now() -  datetime.now()

返回零timedelta为任何人在这里?


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

解决方案

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?

这篇关于从python的午夜开始秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆