Python 3.4 - Calendar and Datetime Modules

Calendar and datetime modules deal with data that you would think they deal with, mainly functions related to the calendar dates, and the amount of days in a month or given year. You can find the date for today or any other day. You can find the amount of days in a given month. You can use this information with some other information to determine when something can begin or finish:

import datetime
import calendar

balance = 5000interest_rate = .13monthly_payment = 500
today = datetime.date.today()  # this sets a variable for today to today
days_in_current_month = calendar.monthrange(today.year, today.month)[1]

days_till_end_month = days_in_current_month - today.day

start_date = today + datetime.timedelta(days=days_till_end_month + 1)

end_date = start_date

while balance > 0:
    interest_charge = (interest_rate / 12) * balance
    balance = balance + interest_charge
    balance = balance - monthly_payment

    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)

2018-06-01 4554.17
2018-07-01 4103.51
2018-08-01 3647.96
2018-09-01 3187.48
2018-10-01 2722.01
2018-11-01 2251.5
2018-12-01 1775.89
2019-01-01 1295.13
2019-02-01 809.16
2019-03-01 317.93
2019-04-01 0
2018-05-08

Some other uses for datetime module:

import datetime

current_weight = 150  # set some variablesgoal_weight = 180avg_lbs_week = 1.5
start_date = datetime.date.today()  # set the start dateend_date = start_date 

while current_weight < goal_weight:
    end_date = end_date + datetime.timedelta(days=7)
    current_weight += avg_lbs_week

print(f'Reached goal in {(end_date - start_date).days // 7} weeks')
print(end_date)

Reached goal in 20 weeks
2018-09-25

Some more:
import datetime
import math

goal_subs = 100000current_subs = 10subs_to_go = goal_subs - current_subs

avg_subs_day = 15
days_to_go = math.ceil(subs_to_go / avg_subs_day)

today = datetime.date.today()

print(f'{today + datetime.timedelta(days= days_to_go)} <<< this is the date')

2036-08-07 <<< this is the date


https://www.youtube.com/watch?v=k8asfUbWbI4

Comments

Popular posts from this blog

Python 3.4 - Caesar Cipher

UML - Use Case Diagram