In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "Python how to achieve Lunar Calendar conversion", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve Lunar Calendar conversion Python" article.
The lunar calendar is the current traditional calendar in China. According to the change cycle of the moon phase, each moon phase is expected to change into one month, the reference solar return year is the length of one year, and 24 solar terms are added and leap months are set to make the average calendar year adapt to the regression year.
For us to deal with data, there is no need to study in detail the conversion relationship between the lunar calendar and the Gregorian calendar. In Python, the ZhDate library supports lunar calendar-Gregorian calendar conversion, date addition and subtraction, and full Chinese date generation. It has built-in lunar calendar data from 1900 to 2100 and relies only on Python built-in modules. Github.com/CutePandaSh/zhdate
Because ZhDate belongs to the third open source module of Python, it requires additional installation and guidance before it can be enabled. Enter the following command directly in the command window:
Detailed usage of pip install zhdate
Zhdate library, directly calculate the Chinese lunar calendar without a network interface, and support the conversion of the lunar calendar to each other.
The date conversion between the Gregorian calendar and the lunar calendar
Enter the following command in an interactive environment:
From zhdate import ZhDatedate1 = ZhDate (2022, 2, 2) # New date object print (date1) of the second day of February 2022 (Dragon head) # directly returns the lunar calendar date string d
Output:
February 2, 2022, lunar calendar
Enter the following command in an interactive environment:
T_date1 = date1.to_datetime () t_date1
Output:
Datetime.datetime (2022, 3, 4, 0, 0)
Enter the following command in an interactive environment:
From datetime import datetimedt_date2 = datetime (2022, 2, 6) date2 = ZhDate.from_datetime (dt_date2) # convert date from Gregorian calendar to lunar date object date2
Output:
January 6, 2022, lunar calendar
Leap month
Enter the following command in an interactive environment:
Date3 = ZhDate (2020, 4, 15) # newly built lunar calendar April 15, 2020 print (date3.to_datetime ())
Output:
2020-05-07 00:00:00
Enter the following command in an interactive environment:
Date3 = ZhDate (2020, 4, 15) # New Lunar Calendar 2020 Leap April 15 print (date3.to_datetime ())
Output:
2020-06-06 00:00:00
Students on April 15 of the lunar calendar can celebrate two lunar birthdays on May 7 and June 6 respectively.
Other
For Chinese output, enter the following commands in an interactive environment:
Dt_date4 = datetime (2022, 3,4) date4 = ZhDate.from_datetime (dt_date4) print (date4.chinese ())
Output:
The year of Ren Yin, the second year of junior high school in February 2022 (year of the Tiger)
For the lunar calendar date of the day, enter the following command in the interactive environment:
ZhDate.today ()
Output:
February 7, 2022, lunar calendar
In addition, the zhdate module supports addition and subtraction. Subtract two zhdate objects to get the difference between two lunar dates, and add an integer to return the new lunar calendar object after the interval of days.
Actual combat: calculate the number of days from the festival
Let's practice this zhdate module through a practical project.
Join us to quickly know how many days away from a certain holiday today? You can use zhdate and datetime libraries to solve this problem.
Enter the following command in an interactive environment:
Today = datetime.now () data_str = today.strftime ('% Ymuri% mmi% d') data_str
Output:
'2022-03-09'
Calculate the number of days away from International Labour Day and enter the following command in an interactive environment:
OneDay = datetime (2022, 5,1) difference = oneDay.toordinal ()-today.toordinal () print (f 'today {data_str} is still {difference} days away from May Day International Labour Day)
Output:
Today, 2022-03-09 is still 53 days away from International Labour Day on May Day.
Calculate the number of days away from the Mid-Autumn Festival and enter the following command in an interactive environment:
OneDay = ZhDate (2022, 8, 15). To_datetime () difference = oneDay.toordinal ()-today.toordinal () print (f 'today {data_str} is still {difference} days away from the Mid-Autumn Festival)
Output:
Today, 2022-03-09 is still 185 days away from the Mid-Autumn Festival.
The above is about the content of this article on "how to convert the lunar calendar in Python". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.