hii readers,
Today I am going to share with you a simple but useful program that can easily find the day from the given date in c++.
Introduction
Many Times In Today's Busy life we have to find the exact week day corresponding to any particular date. For example, with the help of this program, you can easily find the exact day on your date of birth or your readers's date of birth also. Well, I don't want to make this post lengthy so, let's directly try to understand the actual concept behind this script.
Requirements
Basic Knowledge Of C
How it's going to work.
1. First, Take Inputs from User One By One. Date, Month Then Year.
2. Verify User Inputs
3. Find Input Year is a leap year Or Not.
4. Find the weekday using Julien_day Calculation
C++ Example Codes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | #include <iostream> #include <stdlib.h> using namespace std;
/* __author__ = suraj singh bisht __email__ = surajsinghbisht054@gmail.com __Github__ = https://github.com/surajsinghbisht054
#################################################################### # Find Correct Weekday From Exact Date # #################################################################### */
// check day bool check_date(int day, int month){ if ((month==2) && (day>0) && (day<30)){ return true; } if (( (month==1) || (month==3) || (month==5) || (month==7) || (month==8) || (month==10)|| (month==12) ) && ((day>0) && (day<32))) { return true; } if (( (month==4) || (month==6) || (month==9) || (month==11) ) && ((day>0) && (day<31))) { return true; } return false; }
// check month bool check_month(int month){ if ((month>0) && (month<13)){ return true; }else{ return false; } }
// check year bool check_year(int year){
if ((year>999) && (year < 10000)){ return true; }else{
return false; }
} // is it leap year bool check_leap_year(int year){ if (((year % 4)==0) && ((year % 100)!=0)){ return true; }else if ((year % 400)==0){ return true; }else { return false; }
}
// get exact day from date // // http://en.wikipedia.org/wiki/Julian_day#Calculation // const char *get_day(int day, int month, int year){ int JMD; JMD = (day + ((153 * (month + 12 * ((14 - month) / 12) - 3) + 2) / 5) + (365 * (year + 4800 - ((14 - month) / 12))) + ((year + 4800 - ((14 - month) / 12)) / 4) - ((year + 4800 - ((14 - month) / 12)) / 100) + ((year + 4800 - ((14 - month) / 12)) / 400) - 32045) % 7; //cout << JMD; const char *weekday[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; return weekday[JMD]; }
// main trigger function main(){ // instalise variable int year, month, date, x, y;
cout << "\n[+] Welcome To Day Finder Script.";
// Take Inputs cout << "\n[+] Please Enter The Year [YYYY] : "; cin >> year; cout << "\n[+] Verifying Year Format [YYYY] : ";
// verify year if (check_year(year)){ cout << "\n\t[-] Year Verified"; }else{ cout << "\n\t [-] Wrong Year Format\n"; exit(0); }
cout << "\n[+] Please Enter The Month [MM] : "; cin >> month;
// verify month if (check_month(month)){ cout << "\n\t[-] Month Verified"; }else{ cout << "\n\t [-] Wrong Month Format"; exit(0); } cout << "\n[+] Please Enter The Date [DD] : "; cin >> date;
// verify date if (check_date(date, month)){ cout << "\n\t[-] Date Verified\n\n"; }else{ cout << "\n\t [-] Wrong Date Format"; exit(0); } cout << "[+] Date : " << date << ", Month : " << month << ", Year : " << year << " [ "; if (check_leap_year(year)){ cout << "Leap Year" << " ]"; }else{ cout << "Not Leap Year" << " ]"; } cout << "\n\t[-] Weekday : "; cout << get_day(date, month, year); cout << "\n\n"; }
|
Output[+] Welcome To Day Finder Script.
[+] Please Enter The Year [YYYY] : 1991
[+] Verifying Year Format [YYYY] :
[-] Year Verified
[+] Please Enter The Month [MM] : 02
[-] Month Verified
[+] Please Enter The Date [DD] : 15
[-] Date Verified
[+] Date : 15, Month : 2, Year : 1991 [ Not Leap Year ]
[-] Weekday : Friday
Explanation
check_year,
check_month,
check_day functions are created to verify that the user input date, month and year is valid or not. if it's not valid exit the program and if it's valid. procedure forward.
the check_leap_year function is created to find that the user provided year is a leap year or not.
the get_day function is to find the exact week day from given date argument.
Well, Guys, Below codes is one type of formula based on Julian Day Calculation.
JMD = (day + ((153 * (month + 12 * ((14 - month) / 12) - 3) + 2) / 5) +
(365 * (year + 4800 - ((14 - month) / 12))) +
((year + 4800 - ((14 - month) / 12)) / 4) -
((year + 4800 - ((14 - month) / 12)) / 100) +
((year + 4800 - ((14 - month) / 12)) / 400) - 32045) % 7;
After Performing all operation, This key formula returns weekday as a number.
I hope you all enjoyed this tutorial.
Written By :