是否有任何pythonic方式将mysql TO_DAYS输出转换为datetime?

时间:2022-04-02 19:09:55

The output for MySQL function TO_DAYS('2016-04-01') is 736420 (number of days since year 0).

MySQL函数TO_DAYS('2016-04-01')的输出为736420(自0年以来的天数)。

Is there any pythonic way to convert 736420 into datetime object?

是否有任何pythonic方式将736420转换为datetime对象?

1 个解决方案

#1


1  

You could use datetime.fromordinal():

你可以使用datetime.fromordinal():

>>> from datetime import datetime
>>> days = 736420
>>> datetime.fromordinal(days - 365)
datetime.datetime(2016, 4, 1, 0, 0)

As explained in the documentation, January 1 of year 1 has ordinal 1 (and MySQL starts from year 0), so you need to subtract one year.

如文档中所述,第1年1月1日的序号为1(而MySQL从0年开始),因此您需要减去一年。

#1


1  

You could use datetime.fromordinal():

你可以使用datetime.fromordinal():

>>> from datetime import datetime
>>> days = 736420
>>> datetime.fromordinal(days - 365)
datetime.datetime(2016, 4, 1, 0, 0)

As explained in the documentation, January 1 of year 1 has ordinal 1 (and MySQL starts from year 0), so you need to subtract one year.

如文档中所述,第1年1月1日的序号为1(而MySQL从0年开始),因此您需要减去一年。