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

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -