如何将Python datetime对象拆分为日期和时间数字?

时间:2019-03-20 14:58:21

标签: python datetime

我想将Python datetime对象作为日期数字和时间数字存储在数据库中。如何将datetime对象拆分为日期和时间数字?

Today = datetime.today()
TodayNumber = Today.timestamp() #-- this returns a FLOAT

Day = Today.date()  #- date object
Time = Today.time()  #- time object

DayNumber = Day.whatfuntion() #- internal represtation for date in INTEGER
TimeNumber = Time.whatfundtion() #- internal representation for time in INTEGER

更新-日期的解决方案是转换为公序数

DayNumber = Day.toordinal() #- internal represtation for date in INTEGER

1 个答案:

答案 0 :(得分:1)

取决于“日期号”和“时间号”的含义。但是以下方法可以解决您的问题:

>>> from datetime import datetime
>>> year, month, day, hour, minute, second, *_ = datetime.now().timetuple()

有关文档,请参见https://docs.python.org/3/library/datetime.html#datetime.date.timetuple