Python:日期操作代码 [英] Python: Date manipulation code

查看:90
本文介绍了Python:日期操作代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



*对于我,我想要计算一天day_of_a_year天和其对应的月份的差值日,以及月的+1天。提到这个年份是一个已知的变量
eg。

  def a(day_of_year):
< ...>
返回[(days_from_start_of_month),(days_untill_end_of_month)]

so
如果

  day_of_year = 32 
a(32)=(2,28)#assuming day_of_year对应的月份从第30天和第60天。

到目前为止,我正在研究datetime,timeutils和日历模块,真的无法弄清代码的逻辑!我希望我有一些坚实的表现,但是我在timedelta功能的某个地方迷路了。

解决方案

容易建造,下个月的第一天。一旦有了这些,其余的就更容易了。如OP所指出的, calendar.monthrange 函数给我们最可读的方法来获取一个月的最后一天。

 > ;>> from datetime import date,year 
>>>>进口日历
>>>> def first_day(dt):
...#只需将年份和月份复制到新的日期实例
...返回日期(dt.year,dt.month,1)
...
>>> def last_day(dt):
... days_in_month = calendar.monthrange(dt.year,dt.month)[1]
...返回日期(dt.year,dt.month,days_in_month)
...
>>> nth_day = 32
>>> day_of_year = date(2012,1,1)+ timedelta(days = nth_day - 1)
>>>> day_of_year
datetime.date(2012,2,1)
>>> first_day(day_of_year),last_day(day_of_year)
(datetime.date(2012,2,1),datetime.date(2012,2,2))
>>> day_of_year - first_day(day_of_year),last_day(day_of_year) - day_of_year
(datetime.timedelta(0),datetime.timedelta(28))

将这些技术组合成一个函数:

  def delta_to_start_and_end(year,day_of_year) :
dt = date(year,1,1)+ timedelta(days =(day_of_year - 1))

def first_day(dt):
返回日期(dt.year ,dt.month,1)
def last_day(dt):
days_in_month = calendar.monthrange(dt.year,dt.month)[1]
返回日期(dt.year,dt .month,days_in_month)

return(dt - first_day(dt))。days,(last_day(dt) - dt).days

输出:

 >>> delta_to_start_and_end(2012,32)
(0,28)
>>> delta_to_start_and_end(2011,32)
(0,27)
>>> delta_to_start_and_end(2012,34)
(2,26)
>>> delta_to_start_and_end(2012,364)
(28,2)

我不知道您要为这两个值中的每一个添加 1 目前,本月的第一天(第一个例子)将您 0 作为第一个值,并将(month-in-month-1)作为第二个值,因为是这几点之间的差距。如果需要这些功能,在 delta_to_start_and_end 函数的最后一行添加 + 1 是微不足道的。



作为一个历史性的说明,此回答的以前版本使用不同的方法来计算一个月的最后一天,而不使用日历模块:

  def last_day(dt):
rest,month = divmod(dt.month,12)
返回日期(dt.year + rest,month + 1,1) - timedelta(days = 1)

此函数使用 divmod 内置函数来处理'当前月份十二月边缘案在这种情况下,下个月不是 13 ,而是 1 ,我们需要增加一年好。数字回到开始是数字的模数,但是 divmod 函数给我们除数,这恰好是 1 如果当前月份是 12 。这给了我们一个方便的指标,以增加一年。


With python I want to calculate the delta days of a day_of_a_year day and its corresponding month, as well delta days for month + 1.

*Sorry I forgot to mention that the year is a known variable eg.

def a(day_of_year):
    <...>
    return [(days_from_start_of_month),(days_untill_end_of_month)]

so If

day_of_year = 32 
a(32) = (2,28) #assuming the month which the day_of_year corresponds to starts from day 30 and ends to day 60.

So far im studying the datetime , timeutils and calendar modules and I really can't figure out the logic for the code! I wish i had something solid to show, but Im getting lost somewhere in timedelta functions.

解决方案

The first day of the month is easy to construct, as is the first day of the next month. Once you have those, the rest is even easier. As pointed out by the OP, the calendar.monthrange function gives us the most readable method to get the last day of a month.

>>> from datetime import date, year
>>> import calendar
>>> def first_day(dt):
...      # Simply copy year and month into new date instance
...      return date(dt.year, dt.month, 1)
...
>>> def last_day(dt):
...      days_in_month = calendar.monthrange(dt.year, dt.month)[1]
...      return date(dt.year, dt.month, days_in_month)
...
>>> nth_day = 32
>>> day_of_year = date(2012, 1, 1) + timedelta(days=nth_day - 1)
>>> day_of_year
datetime.date(2012, 2, 1)
>>> first_day(day_of_year), last_day(day_of_year)
(datetime.date(2012, 2, 1), datetime.date(2012, 2, 29))
>>> day_of_year - first_day(day_of_year), last_day(day_of_year) - day_of_year
(datetime.timedelta(0), datetime.timedelta(28))

To combine these techniques into one function:

def delta_to_start_and_end(year, day_of_year):
    dt = date(year, 1, 1) + timedelta(days=(day_of_year - 1))

    def first_day(dt):
         return date(dt.year, dt.month, 1)
    def last_day(dt):
         days_in_month = calendar.monthrange(dt.year, dt.month)[1]
         return date(dt.year, dt.month, days_in_month)

    return (dt - first_day(dt)).days, (last_day(dt) - dt).days

Output:

>>> delta_to_start_and_end(2012, 32)
(0, 28)
>>> delta_to_start_and_end(2011, 32)
(0, 27)
>>> delta_to_start_and_end(2012, 34)
(2, 26)
>>> delta_to_start_and_end(2012, 364)
(28, 2)

I'm not sure if you want to add 1 to each of these two values; currently the first day of the month (first example) gives you 0 as the first value and (days-in-the-month - 1) as the second value, as this is the difference in days from those points. It's trivial to add + 1 twice on the last line of the delta_to_start_and_end function if you need these.

As a historic note, a previous version of this answer used a different method to calculate the last day of a month without the calendar module:

def last_day(dt):
     rest, month = divmod(dt.month, 12)
     return date(dt.year + rest, month + 1, 1) - timedelta(days=1) 

This function uses the divmod builtin function to handle the 'current month is December' edge-case; in that case the next month is not 13, but 1 and we'd need to increase the year by one as well. Rolling over a number back to the 'start' is the modulus of the number, but the divmod function gives us the divisor as well, which happens to be 1 if the current month is 12. This gives us a handy indicator when to increase the year.

这篇关于Python:日期操作代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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