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
Post a Comment