Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use datetime Module in python

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to use the datetime module in python, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Create a new python file named py3_calc_date.py, and write the operation code in this file:

The first example of import datetimeimport calendar#: # calculate how long it will take to pay off the credit card # define the total amount of credit card outstanding balance = 1000 yuan, define the interest rate as 13%interest_rate = 13 *. 0 yuan, define monthly_payment = 50 yuan per month to get today's today = datetime.date.today () # print (today) # get how many days this month days_in_current_month = calendar.monthrange (today.year) Today.month) [1] # returns a tuple # the first parameter is the day of the week # the second parameter is the number of days this month print (days_in_current_month) # get a few days to the end of the month days_till_end_month = days_in_current_month-today.dayprint (days_till_end_month) # get the start date of next month # as our first repayment month start_date = today + datetime.timedelta ( Days=days_till_end_month + 1) print (start_date) # defines the repayment date to be equal to the starting date end_date = start_datewhile balance > 0: # the cost of obtaining interest interest_charge = (interest_rate / 12) * balance # the total amount required to be repaid balance + = interest_charge # minus the amount repaid Remaining balance to be returned-= monthly_payment # rounded to keep two small digits balance = round (balance,2) if balance

< 0: balance = 0 #打印还款月,剩余待还金额 print(end_date,balance) #获取还款月的总天数 days_in_current_month = calendar.monthrange(end_date.year,end_date.month)[1] #获取下一还款月 end_date = end_date + datetime.timedelta(days=days_in_current_month) #运行就可以查看到需要多少个月还清信用卡了 #第二个例子:#算一下多少周可以减肥到目标体重#当前体重150斤current_weight = 150#目标体重100斤goal_weight = 100#假设一周减掉1.5斤avg_lose_weight_week = 1.5#获取开始日期 start_date = datetime.date.today()#定义达到目标体重的结束日期end_date = start_datewhile current_weight >

Goal_weight: current_weight-= avg_lose_weight_week end_date + = datetime.timedelta (days=7) print (end_date) print (f 'needs {(end_date-start_date). Days / / 7} weeks!)

# third example # calculate when fans can reach the target number of 10,000 fans goal_subs = 10,000 million current number of fans current_subs = 8500 fans and how many more fans need to reach the target subs_to_goal = goal_subs-current_subs# assume that the number of fans per day increases to 200 avg_subs_day = 200import math# calculate how many days it takes to reach the target days_to_goal = math.ceil (subs_to_goal/avg_subs_day ) # calculate when the target is reached today = datetime.date.today () goal_subs_date = today + datetime.timedelta (days=days_to_goal) print (goal_subs_date) above is how to use the datetime module in python The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report