如何找出两个日期之间的年份差异? [英] How to find difference in years between two dates?

查看:78
本文介绍了如何找出两个日期之间的年份差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了找到某人的年龄,我正在寻找一种方法来获得
找到两个日期之间年份的差异,所以我可以这样做

类似于:


age =(date.today() - born)。年


但是没有用(timedelta)上课没有一年

accessor)。


我查看了文档和食谱,但我找不到任何东西,所以

我想出了:


def age(天生):

now = date.today()

birthday = date(now.year,born.month,born.day)

现在返回.year - born.year - (生日现在和1或0)


即计算原始如果他今年没有生日那么多,那么他会先减去一个......但是我会选择标准的

和泛型如果存在的方法......?


- bjorn

For the purpose of finding someone''s age I was looking for a way to
find how the difference in years between two dates, so I could do
something like:

age = (date.today() - born).year

but that didn''t work (the timedelta class doesn''t have a year
accessor).

I looked in the docs and the cookbook, but I couldn''t find anything, so
I came up with:

def age(born):
now = date.today()
birthday = date(now.year, born.month, born.day)
return now.year - born.year - (birthday now and 1 or 0)

i.e. calculate the "raw" years first and subtract one if he hasn''t had
his birthday yet this year... It works, but I''d rather use a standard
and generic approach if it exists...?

-- bjorn

推荐答案

thebjorn写道:
thebjorn wrote:

为了找到某人的年龄我正在寻找一种方法来获取
如何找出两年之间的差异约会,所以我可以做

类似于:


age =(date.today() - born).year


但是没有用(timedelta课程没有一年

访问者)。


我查看了文档和食谱,但我找不到任何东西,所以

我想出了:


def age(天生):

now = date.today()

birthday = date(now.year,born.month,born.day)
For the purpose of finding someone''s age I was looking for a way to
find how the difference in years between two dates, so I could do
something like:

age = (date.today() - born).year

but that didn''t work (the timedelta class doesn''t have a year
accessor).

I looked in the docs and the cookbook, but I couldn''t find anything, so
I came up with:

def age(born):
now = date.today()
birthday = date(now.year, born.month, born.day)



如果下注者出生,运气不好在2月29日和今年不是闰年的b $ b。

Bad luck if the punter was born on 29 Feb and the current year is not a
leap year.


现在返回.year - born.year - (生日现在和1或0)
return now.year - born.year - (birthday now and 1 or 0)



神圣的代码臃肿,蝙蝠侠!试试这个:


现在返回。年 - born.year - (现在过生日)

Holy code bloat, Batman! Try this:

return now.year - born.year - (birthday now)


>

即计算原始如果他今年没有生日那么多,那么他会先减去一个......但是我会选择标准的

和泛型如果它存在的方法......?
>
i.e. calculate the "raw" years first and subtract one if he hasn''t had
his birthday yet this year... It works, but I''d rather use a standard
and generic approach if it exists...?



这是造成问题的不规则月份。如果你可以工作

的月份差异,那么只需将floor_div减去12来获得年份

的差异。


下面是从远古时代开始,每个人和他的狗

都有自己的日期等级:-)


HTH,

约翰


8< ---日期类的方法


def months_until(self,to_date):

"""返回from_date(self)和to_date之间的月数。

"""

from_date = self

signum = 1

如果from_date to_date:

from_date,to_date = to_date,from_date

signum = -1

d1,m1,y1 = from_date.day,from_date.month,from_date.year

d2,m2,y2 = to_date.day,to_date.month,to_date.year

mdiff =(y2-y1)* 12 + m2-m1

如果d2< d1和(d2 <28或d2!= last_day_of_month(y2,m2)):

#测试d2< 28不是必需的;这是一个优化

#以避免不必要地调用last_day_of_month

mdiff = mdiff - 1

返回mdiff * signum

def years_until(self,to_date):

"""返回from_date(self)和to_date之间的年数。

"" ;"

md = self.months_until(to_date)

如果md> = 0:

return md // 12
else:

#确保除法截断为零

返回 - (( - md)// 12)


8< ---模块级功能和常量


#个月天数

_dim =(无,31,28,31,30, 31,30,31,31,30,31,30,31)

def last_day_of_month(y,m):

"""返回日(1..31),这是y年的最后一天m

"""

如果m == 2:

返回28 + _leap(y)

否则:

如果不是(1< = m< = 12):

引发DateError,月份不在1..12"

返回_dim [m]


def _leap(y):

如果y%4 :返回0

如果y%100:返回1

如果y%400:返回0

返回1

8< ----

It''s the irregular-size months that cause the problems. If you can work
out the months difference, then just floor_div by 12 to get the years
difference.

Below is some code from the ancient times when everybody and his dog
each had their own date class :-)

HTH,
John

8<--- methods from a date class

def months_until(self, to_date):
"""Return number of months between from_date (self) and to_date.
"""
from_date = self
signum = 1
if from_date to_date:
from_date, to_date = to_date, from_date
signum = -1
d1, m1, y1 = from_date.day, from_date.month, from_date.year
d2, m2, y2 = to_date.day, to_date.month, to_date.year
mdiff = (y2 - y1) * 12 + m2 - m1
if d2 < d1 and (d2 < 28 or d2 != last_day_of_month(y2, m2)):
# the test d2 < 28 is not necessary; it is an optimisation
# to avoid calling last_day_of_month unnecessarily
mdiff = mdiff - 1
return mdiff * signum

def years_until(self, to_date):
"""Return number of years between from_date (self) and to_date.
"""
md = self.months_until(to_date)
if md >= 0:
return md // 12
else:
# ensure division truncates towards zero
return -((-md) // 12)

8<--- module-level functions and constants

# days in month
_dim = (None, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

def last_day_of_month(y, m):
"""Return day (1..31) which is last day of month m in year y
"""
if m == 2:
return 28 + _leap(y)
else:
if not (1 <= m <= 12):
raise DateError, "month not in 1..12"
return _dim[m]

def _leap(y):
if y % 4: return 0
if y % 100: return 1
if y % 400: return 0
return 1

8<----


thebjorn写道:
thebjorn wrote:

For找到某人的年龄的目的我正在寻找一种方法来找到两个日期之间的年份差异,所以我可以这样做:/ b
: br />

年龄=(date.today() - 出生)。年


但这不起作用(timedelta课程没有'''有一年

accessor)。


我查看了文档和食谱,但我找不到任何东西,所以

我想出了:


def age(天生):

now = date.today()

birthday = date(now.year,born.month,born.day)

现在返回.year - born.year - (现在生日,1或0)


即计算原始数据。如果他今年没有生日那么多,那么他会先减去一个......但是我会选择标准的

和泛型如果它存在的方法......?
For the purpose of finding someone''s age I was looking for a way to
find how the difference in years between two dates, so I could do
something like:

age = (date.today() - born).year

but that didn''t work (the timedelta class doesn''t have a year
accessor).

I looked in the docs and the cookbook, but I couldn''t find anything, so
I came up with:

def age(born):
now = date.today()
birthday = date(now.year, born.month, born.day)
return now.year - born.year - (birthday now and 1 or 0)

i.e. calculate the "raw" years first and subtract one if he hasn''t had
his birthday yet this year... It works, but I''d rather use a standard
and generic approach if it exists...?



你可能想看看mxDatetime,它有一个RelativeDateTime

类型似乎可以做你想要的:
http://www.egenix.com/files/python/mxDateTime .html

-

bruno desthuilliers

python -c" print''@''。join([' '。''。加入([w [:: - 1] for p in p.split(''。'')])for'/ b $ bp in''o **** @ xiludom。 gro''。split(''@'')])"

You may want to have a look at mxDatetime, which has a RelativeDateTime
type that seems to do what you want:
http://www.egenix.com/files/python/mxDateTime.html
--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"


" thebjorn" < bp ************** @ gmail.comwrote:
"thebjorn" <bp**************@gmail.comwrote:

def age(born):

now = date.today()

birthday = date(now.year,born.month,born.day)

现在返回.year - born.year - (现在生日和1或0)
def age(born):
now = date.today()
birthday = date(now.year, born.month, born.day)
return now.year - born.year - (birthday now and 1 or 0)



我没有得到最后一行。特别是有两件事让我感到困惑。


1)生日快乐是什么意思?听起来你还没出生过




2)我找到了和1或0。部分非常混乱。我不记得关于运算符优先级的所有

小规则,但我确定这对于一些涉及布尔短路评估的一些聪明的黑客攻击来解决在python中绕过

缺少三元运算符。如果我需要提取参考资料

手册来破译表达式的含义,那就太复杂了。尝试

类似于:


如果现在生日:

现在返回。年 - 生日。年 - 1

其他:

现在返回。年 - 生日。白色


它需要更多的空间,但它很容易理解没有

摸不着头脑或潜入手册以刷新你的记忆

模糊的语言细节。

I don''t get that last line. There''s two things in particular that are
puzzling me.

1) What does "birthday now" mean? It sounds like you haven''t been born
yet.

2) I find the "and 1 or 0" part very confusing. I can''t remember all the
minor rules about operator precedence, but I''m sure this works out to some
clever hack involving boolean short-circuit evaluation to get around the
lack of a ternary operator in python. If I need to pull out the reference
manual to decipher what an expression means, it''s too complicated. Try
something like:

if birthday now:
return now.year - born.year - 1
else:
return now.year - born.year

It takes up a little more space, but it''s bog easy to understand without
scratching your head or diving into the manual to refresh your memory of
obscure language details.


这篇关于如何找出两个日期之间的年份差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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