python - Is there a way to convert a variable that can be either a datetime or a date to a date? -


i have function can accept date or datetime 1 of arguments. if value datetime, convert date components. pythonic way of doing that?

since datetime objects are date objects, should able call date functions on datetime , have work.

but if must:

if isinstance(param_date, datetime.datetime):      param_date = param_date.date() 

will normallize datetime , date objects date objects... have deal them, date objects not datetime objects:

>>> isinstance(datetime.date.today(), datetime.datetime) false 

note: reverse not applicable:

>>> isinstance(datetime.datetime.now(), datetime.date) true 

as datetime inherits date.


Comments

Popular posts from this blog

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

javascript - Confirm a form & display message if form is valid with JQuery -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -