在Python中找到两个日期之间的中点日期? [英] Finding mid-point date between two dates in Python?

查看:328
本文介绍了在Python中找到两个日期之间的中点日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定:

my_guess = '2014-11-30'
their_guess = '2017-08-30'

如何在日期之间拆分增量,返回正确的日历日期。

How do I split the delta between the dates, returning the correct calendar date.

推荐答案

一种方法是使用 datetime 。查找两个日期之间的差异,将其减半,并将其添加到较早的日期:

One way would be to use datetime. Find the difference between two dates, halve it, and add it on the earlier date:

>>> from datetime import datetime
>>> a = datetime(2014, 11, 30)
>>> b = datetime(2017, 8 ,30)
>>> a + (b - a)/2
2016-04-15 00:00:00

这篇关于在Python中找到两个日期之间的中点日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆