[Python] Calculate the total number of days in a year (and New Year’s Eve dates)

It took me a while to find the format code for finding the total day of the year in Python, so I also recorded the calculation method.

basic

First, simply find the total number of days in the year,

The format code for strftime() and strptime() is %j

↓Lookhere
Conversion using strftime(‘%j’) returns a 3-digit total number of days padded with zeros.
Then you can use int() to convert it to a numerical value.

import datetime

def calc():

    day = datetime.date(2023, 4, 1)
    total_day = day.strftime('%j')

    print(f'2023/04/01的总天数 = {
      
      total_day}')

    total_int = int(total_day)
    print(f'用 int 处理 : {
      
      total_int}')

if __name__ == '__main__':
    calc()
$ python test1.py
2023/04/01的总天数 = 091int 处理 : 91

leap year

Change the year and see the total number of days in the leap year.

import datetime

def calc():

    day = datetime.date(2020, 4, 1)
    total_day = day.strftime('%j')

    print(f'2020/04/01的总天数 = {
      
      total_day}')

if __name__ == '__main__':
    calc()
$ python test2.py
2020/04/01的总天数 = 092

The total number of days in 2023/04/01 is 91, so you can see that 2/29 in the leap year has also been added correctly.

The total number of days to process the New Year's Eve

For those using 366 (367), or a greater value in the year, or when dealing with the total number of days from the end of the year to the beginning of the year, set the value for the end of the previous year to a negative number and the value for next January to 366 or higher case

Basically, you just need to add 365 to the total number of days in the next year, but you need to consider whether the previous year is a leap year, so you need to add the total number of days on December 31 of the previous year (365 or 366).

import datetime

def calc(year):

    next_year = year + 1  # 定义明年

    day = datetime.date(year, 12, 1)  # 前年12/1
    additional_day = datetime.date(year, 12, 31)  # 追加的12/31
    spanning_year = datetime.date(next_year, 1, 3)  # 想要跨年计算的天数

    total_day = day.strftime('%j')
    additional_total_day = int(additional_day.strftime('%j'))
    next_year_total_day = int(spanning_year.strftime('%j'))
    sppaning_total_day = additional_total_day + next_year_total_day

    print(f'{
      
      year}/12/01的总天数 = {
      
      total_day}')
    print(f'{
      
      year}/12/31的总天数 = {
      
      additional_total_day}')
    print(f'{
      
      next_year}/01/03的总天数 = {
      
      sppaning_total_day}')

if __name__ == '__main__':
    print('======== 平年 ========')
    calc(2022)
    print('====== 闰年考虑 ======')
    calc(2020)
$ python test_spanning_year1.py
======== 平年 ========
2022/12/01的总天数 = 335
2022/12/31的总天数 = 365
2023/01/03的总天数 = 368
====== 闰年考虑 ======
2020/12/01的总天数 = 336
2020/12/31的总天数 = 366
2021/01/03的总天数 = 369

You can see that the leap year of 2020 has been taken into account and an extra day has been added.

Make December of the previous year a negative value

The final calculation is a bit complicated.
First, since it can take negative values, you need to decide where to place the zeros.
If you ignore zeros and don't care if 1/1 is the first day and 12/31 of the previous year is -1 day, that's fine, but when writing, the difference of 1 day Looks like 2 days difference. I think there might be some inconvenience.

This time I want to use negative numbers to express the past year, so
・December 31st of the previous year is -1.
・1/1 is day 0
・The total number of days after January 1st is (normal total number of days - 1) days.
Calculate it like this.

If you want to express the previous year as a negative number, you can basically use (total number of days - 365 - 1) to express it, but like the +365 example in the previous section, you need to take into account the following: the previous year is a leap year, and the calculation formula is (total number of days - 12/31 of the previous year, total number of days - 1).

$ python test_spanning_year2.py
======== 平年 ========
2023/01/05的总天数 = 4
2022/12/31的总天数 = 365
2022/12/27的总天数 = -5
====== 闰年考慮 ======
2021/01/05的总天数 = 4
2020/12/31的总天数 = 366
2020/12/27的总天数 = -5

It's hard to tell if the result is correct, so make a table of hypothetical values ​​and check.
Of the two years, 1/1 should be 0 and 12/31 should be -1, so the correct total dates are as shown in the table below. Insert image description hereConsistent with the calculation results!

Guess you like

Origin blog.csdn.net/Allan_lam/article/details/134994387