This app checks for future dates, and checks for Leap Year without using predefined date and time methods.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace _1031FinalProject { public partial class futureDate : Form { public futureDate() { InitializeComponent(); } // Start // Declare variables int[] day30 = { 4, 6, 9, 11 }; int[] leapYear = { 2000, 2004, 2008, 2012, 2016, 2020, 2024 }; int intMonth = 0; int intDay = 0; int intYear = 0; int intHour = 0; int intHour12 = 0; int intMinute = 0; int intEndDay = 0; int intEndHour = 0; string outputMessage = ""; int daysInThisMonth = 31; int daysIn2ndMonth = 31; int daysIn3rdMonth = 31; int hourAdd; int hoursToDays; int totalHour; int daysToMonths; int totalDay; int dayAdd; int monthAdd; int totalMonth; int totalYear; ///// Debug... test variables string errorOutput = ""; Boolean deBug = false; // true turns Debugging mode on // false turns it off //////// private void futureDate_Load(object sender, EventArgs e) { // Create Start Hours (1 to 12) hourStartBox.Items.Clear(); for (int i = 1; i <= 12; i++) { hourStartBox.Items.Add(i); } // Create Start Minuts (0 to 59) minuteStartBox.Items.Clear(); for (int i = 0; i <= 59; i++) { minuteStartBox.Items.Add(i); } // Create End Days (1 to 60) dayEndBox.Items.Clear(); for (int i = 1; i <= 60; i++) { dayEndBox.Items.Add(i); } // Create End Hours (0 to 24) hourEndBox.Items.Clear(); for (int i = 0; i <= 24; i++) { hourEndBox.Items.Add(i); } } //////////////////////////////////// // Load months after year is selected private void yearStartBox_SelectedIndexChanged(object sender, EventArgs e) { // clear Month and Day so wrong values can't be chosen if already // entered and later want to change the year ( may change it to a Leap Year) monthStartBox.SelectedValue = ""; monthStartBox.Text = "Month"; monthStartBox.Items.Clear(); dayStartBox.SelectedValue = ""; dayStartBox.Text = "Day"; dayStartBox.Items.Clear(); // get value of the Year intYear = yearStartBox.SelectedIndex + 2000; ///// Debug... test month output if (deBug == true) { string yearTest = "the output of YEAR is: " + intYear + "\n"; errorOutput += yearTest; outputLabel.Text = errorOutput; } //////////////////// // Create Months monthStartBox.Items.Clear(); if (intYear > 0) { for (int i = 1; i <= 12; i++) { monthStartBox.Items.Add(i); } } } //////////////////////////////////// // Load days after Month is selected private void monthStartBox_SelectedIndexChanged(object sender, EventArgs e) { // clear Days so wrong values can't be chosen if already // entered and later want to change the month ( days in month varry) dayStartBox.SelectedValue = ""; dayStartBox.Text = "Day"; dayStartBox.Items.Clear(); // get value of the month intMonth = monthStartBox.SelectedIndex + 1; ///// Debug... test month output if (deBug == true) { string monthTest = "the output of MONTH is: " + intMonth + "\n"; errorOutput += monthTest; outputLabel.Text = errorOutput; } //////////////////// // Create the days with 31 days dayStartBox.Items.Clear(); if (intMonth > 0) { for (int i = 1; i <= 31; i++) { dayStartBox.Items.Add(i); } } // check for months with only 30 days for (int a = 0; a < day30.Length; a++) { if (intMonth == day30[a]) { daysInThisMonth = 30; dayStartBox.Items.Clear(); for (int i = 1; i <= 30; i++) { dayStartBox.Items.Add(i); } } } // this checks for 30 days in 2nd month for (int a = 0; a < day30.Length; a++) { if ((intMonth + 1) == day30[a]) { daysIn2ndMonth = 30; } } // this checks for 30 days in 3nd month for (int a = 0; a < day30.Length; a++) { if ((intMonth + 2) == day30[a]) { daysIn3rdMonth = 30; } } // check for February, it only has 28 days if (intMonth == 2 | intMonth == 14) { daysInThisMonth = 28; dayStartBox.Items.Clear(); for (int i = 1; i <= 28; i++) { dayStartBox.Items.Add(i); } // check for Leap Year! it gives February 29 days for (int a = 0; a < leapYear.Length; a++) { if (intYear == leapYear[a]) { daysInThisMonth = 29; dayStartBox.Items.Clear(); for (int i = 1; i <= 29; i++) { dayStartBox.Items.Add(i); } } } } // this checks for 28 days in 2nd month // also check if year roles over if (intMonth > 10) { int plusYear = intYear + 1; if ((intMonth + 1) == 2 | (intMonth + 1) == 14) { daysIn2ndMonth = 28; // check for Leap Year! it gives February 29 days for (int a = 0; a < leapYear.Length; a++) { if (plusYear == leapYear[a]) { daysIn2ndMonth = 29; } } } // this checks for 28 days in 3nd month if ((intMonth + 2) == 2 | (intMonth + 2) == 14) { daysIn3rdMonth = 28; // check for Leap Year! it gives February 29 days for (int a = 0; a < leapYear.Length; a++) { if (plusYear == leapYear[a]) { daysIn3rdMonth = 29; } } } } else { if ((intMonth + 1) == 2 | (intMonth + 1) == 14) { daysIn2ndMonth = 28; // check for Leap Year! it gives February 29 days for (int a = 0; a < leapYear.Length; a++) { if (intYear == leapYear[a]) { daysIn2ndMonth = 29; } } } // this checks for 28 days in 3nd month if ((intMonth + 2) == 2 | (intMonth + 2) == 14) { daysIn3rdMonth = 28; // check for Leap Year! it gives February 29 days for (int a = 0; a < leapYear.Length; a++) { if (intYear == leapYear[a]) { daysIn3rdMonth = 29; } } } } } private void dayStartBox_SelectedIndexChanged(object sender, EventArgs e) { // get value of the Day intDay = dayStartBox.SelectedIndex + 1; ///// Debug... test Day output if (deBug == true) { string dayTest = "the output of DAY is: " + intDay + "\n"; errorOutput += dayTest; outputLabel.Text = errorOutput; } } private void hourStartBox_SelectedIndexChanged(object sender, EventArgs e) { // get value of the Hour // 12:35am does not compleate a full hour, so make 12 = 0 intHour12 = hourStartBox.SelectedIndex + 1; if (intHour12 == 12) { intHour = 0; } else { intHour = intHour12; } ///// Debug... test Hour output if (deBug == true) { string hourTest = "the output of HOUR is: " + intHour + "\n"; errorOutput += hourTest; outputLabel.Text = errorOutput; } } private void minuteStartBox_SelectedIndexChanged(object sender, EventArgs e) { // get value of the Minute intMinute = minuteStartBox.SelectedIndex; ///// Debug... test Minute output if (deBug == true) { string minuteTest = "the output of MINUTE is: " + intMinute + "\n"; errorOutput += minuteTest; outputLabel.Text = errorOutput; } } private void dayEndBox_SelectedIndexChanged(object sender, EventArgs e) { // get value of the END Days intEndDay = dayEndBox.SelectedIndex + 1; ///// Debug... test Minute output if (deBug == true) { string dayEndTest = "the output of END DAY is: " + intEndDay + "\n"; errorOutput += dayEndTest; outputLabel.Text = errorOutput; } } private void hourEndBox_SelectedIndexChanged(object sender, EventArgs e) { // get value of the END Hour intEndHour = hourEndBox.SelectedIndex; ///// Debug... test Minute output if (deBug == true) { string hourEndTest = "the output of END HOUR is: " + intEndHour + "\n"; errorOutput += hourEndTest; outputLabel.Text = errorOutput; } } private void startButton_Click(object sender, EventArgs e) { // clear messages errorOutput = ""; outputMessage = ""; // Check am and pm radio string stringAmPm = ""; int intAmPm; if (amRadio.Checked == true) { intAmPm = 0; stringAmPm = "am"; } else if (pmRadio.Checked == true) { intAmPm = 12; stringAmPm = "pm"; } else // if niether radio is checked leave a message and stop exicution { errorOutput += "AM/PM Radio button not checked!"; outputMessage += "Please choose AM or PM"; outputLabel.Text = outputMessage; return; } // if nothing is entered if ((intYear + intMonth + intDay + intHour) < 1) { outputMessage += "This is about the time Jesus our Messiah was born!" + "\n***Please enter starting point information***\n\n"; } else { if ((intEndDay + intEndHour) < 1) { outputMessage += "You want to know about the future right?... " + "\n***Please enter ending point information***\n\n"; } else if (intEndDay < 1) outputMessage += "*Enter the END DAY*\n"; if (intYear < 1) outputMessage += "*Enter the start YEAR*\n"; if (intMonth < 1) outputMessage += "*Enter the start MONTH*\n"; if (intDay < 1) outputMessage += "*Enter the start DAY*\n"; } ///// Debug... test all variables if (deBug == true) { string a = ", "; errorOutput = intYear + a + intMonth + a + intDay + a + intHour + a + intMinute + a + stringAmPm + a + intEndDay + a + intEndHour + a + daysInThisMonth + a + daysIn2ndMonth + a + daysIn3rdMonth; outputLabel.Text += errorOutput; } //////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// // --THE MATH-- /////////////////////////////////////////////////////// // start with add hours and am/pm (+12 hours for pm) hourAdd = intHour + intEndHour + intAmPm; // enough hours to change the day? whats left over? hoursToDays = hourAdd / 24; totalHour = hourAdd % 24; // am or pm? if (totalHour > 12) { stringAmPm = "pm"; totalHour -= 12; } else if (totalHour == 0) // put back into 12:00 am { totalHour = 12; stringAmPm = "am"; } // add up the days dayAdd = hoursToDays + intDay + intEndDay; //Now it gets tricky ---- we can have up to a 92 day change, thats 3 months with diferent ending days // enough days to change the month? whats left over? //int daysToMonths = dayAdd / daysInThisMonth ; // no good here //int totalDay = dayAdd % daysInThisMonth; // no good here // add up the months ----//////---- how many days are in the next two months? if (dayAdd <= daysInThisMonth) { daysToMonths = 0; totalDay = dayAdd; } else if (dayAdd <= (daysInThisMonth + daysIn2ndMonth)) { daysToMonths = 1; totalDay = dayAdd - daysInThisMonth; } else if (dayAdd <= (daysInThisMonth + daysIn2ndMonth + daysIn3rdMonth)) { daysToMonths = 2; totalDay = dayAdd - (daysInThisMonth + daysIn2ndMonth); } else { daysToMonths = 3; totalDay = dayAdd - (daysInThisMonth + daysIn2ndMonth + daysIn3rdMonth); } // add to the Months monthAdd = daysToMonths + intMonth; // did we change years? if (monthAdd > 12) { totalYear = intYear + 1; totalMonth = monthAdd - 12; } else { totalYear = intYear; totalMonth = monthAdd; } // some how i deleted some code, the whole else statment (above) was gone, meybe more // Make the output message outputMessage += "\nYOUR FUTURE DATE IS \n\n" + totalYear + " / " + totalMonth + " / " + totalDay + "\n" + totalHour + ":" + intMinute.ToString("00") + " " + stringAmPm + "\n\n\n\nThank You, Have a great day!"; ///// Debug... test month output if (deBug == true) { errorOutput += "... hours added up: " + hourAdd + ", hours to days: " + hoursToDays + ", Total hours left: " + totalHour + ", days added up: " + dayAdd + ", days to months: " + daysToMonths + ", total days left: " + totalDay + ", total months: " + totalMonth + ", total year: " + totalYear + ", AM/PM is: " + stringAmPm + "\n" + totalYear + " / " + totalMonth + " / " + totalDay + "\n" + totalHour + ":" + intMinute + " " + stringAmPm; outputLabel.Text = errorOutput; } else { outputLabel.Text = outputMessage; } } //////////////////////////////////// // RESET FIELDS private void resetButton_Click(object sender, EventArgs e) { // reset feilds yearStartBox.Text = "Year"; monthStartBox.Text = "Month"; monthStartBox.Items.Clear(); dayStartBox.Text = "Day"; dayStartBox.Items.Clear(); hourStartBox.Text = "Hour"; minuteStartBox.Text = "Min."; dayEndBox.Text = "Day"; hourEndBox.Text = "Hour"; amRadio.Checked = false; pmRadio.Checked = false; // reset messages errorOutput = ""; outputMessage = ""; outputLabel.Text = errorOutput; // reset variables intMonth = 0; intDay = 0; intYear = 0; intHour = 0; intHour12 = 0; intMinute = 0; intEndDay = 0; intEndHour = 0; daysInThisMonth = 31; daysIn2ndMonth = 31; daysIn3rdMonth = 31; hourAdd = 0; hoursToDays = 0; totalHour = 0; daysToMonths = 0; totalDay = 0; dayAdd = 0; monthAdd = 0; totalMonth = 0; totalYear = 0; } } }
