
Python
Pythonimport datetimedef date_diff(start_date, end_date, unit): delta = (end_date - start_date).days if unit == "D": return delta elif unit == "Y": return delta * 365 elif unit == "M": return delta * 12这个函数首先获取起始日期和终止日期之间的差值(以天数为单位),然后根据所需计数单位进行相应调整。如果需要计算其他类型的差异(如年或月),也可以在函数参数中进行更改。请注意,在使用这个函数之前,请确保起始和终止日期都已经被正确赋值,并且它们在同一种时间格式下。否则可能会导致错误结果。希望这个参考函数能够满足您的需求!如需进一步帮助,请随时告知。Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号