Python: Date manipulation code

后端 未结 2 1096
南笙
南笙 2021-01-28 10:25

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

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-28 11:02

    I don't think that there's an existing library that works for this. You have to make something yourself, like this:

    monthdays = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
    
    day = 32
    
    total = 0
    for i in monthdays:
        if day - total - i < 0:
            before = day - total
            after = total + i - day
            break
        total += i
    
    print before, after
    

    (just a quick start, there is possibly a more elegant way)

提交回复
热议问题