CW Gan
CW Gan

Reputation: 23

How to split Python datetime object to date and time numbers?

I would like to store a Python datetime object as date number and time number in a database. How do you split the datetime object into date and time numbers ?

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

Update - the slution for date is to convert to the gregorial oridnal

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

Upvotes: 1

Views: 820

Answers (1)

Martin Indra
Martin Indra

Reputation: 126

Depends on what you mean by "date number" and "time number". But the following may solve your issue:

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

See https://docs.python.org/3/library/datetime.html#datetime.date.timetuple for documentation.

Upvotes: 1

Related Questions