Last active
December 7, 2017 06:33
-
-
Save chaewonkong/647b6a93328c25033879eedc0423dd1a to your computer and use it in GitHub Desktop.
What Day Calculator, get 8 digit numbers of yyyymmdd then prints what day is yyyymmdd.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| '''What Day Calculator based on 8 digits (yyyymmdd) | |
| Input of 8 digits(first four for year, next two for month, last two for day) | |
| Output = the exact day (like "Monday" or "Saturday") | |
| Remember, 0001 Jan 1st was Monday! | |
| ''' | |
| #Get an input: first 4 digits for year, next 2 digits for month, last 2 digits for day. | |
| #The input will be saved as a string | |
| date = input("Please enter a date an year in 8 digits ex) 19920303:") | |
| print("Your input:", date) | |
| #Split the string of input as a integer. | |
| year = int(date[:4]) | |
| month = int(date[4:6]) | |
| day = int(date[6:]) | |
| #ord_year means ordinary year, weareas leap_year means leap year. | |
| ord_year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] | |
| leap_year = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] | |
| what_day=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] | |
| #var num_day = number of days between input and "00010101" | |
| #Firstly, calcultate num_day on year to year basis. | |
| #Then, calculate surplus days by month and day and add it to the original num_day | |
| leap = 0 | |
| #AD1 means Anno Domini 1. The First Year | |
| AD1 = 1 | |
| #If input date = 00010101, just print "Monday" | |
| if date == '00010101': | |
| print("Monday") | |
| #From the input, let's calculate what day was it | |
| else: | |
| for i in range(AD1, year+1): | |
| if i % 400 == 0 or i % 4 == 0 and i % 100 != 0: | |
| leap +=1 | |
| num_day = (year -AD1 - leap)*365 + leap*366 | |
| if year % 400 == 0 or year % 4 == 0 and year % 100 == 0: | |
| # leap_year | |
| num_day += sum(leap_year[:(month - 1)]) + (day-1) | |
| else: | |
| # ord_year | |
| num_day += sum(ord_year[:(month - 1)]) + (day-1) | |
| print(what_day[(num_day%7)]) | |
| print("Good Bye~") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment