Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Simple App to Make Julian Date Calculator

0.00/5 (No votes)
14 Oct 2015 1  
A basic Windows Forms Desktop Application to return a Julian Date

Introduction

Here is a simple Form Application to calculate the Julian Date. This is a program that consists of just a Form and a single Class. This is a basic tip on how to make a small app that is event driven.

Background

I made this app as I used a site to find the Julian Date and recently the site took the Julian Calendar down, so I did not want to look up a pdf every time I needed to find a date. I had a few hours free and I decided to make a Julian Date Calendar.

Using the Code

The program is designed to be a Form Application that can calculate a Julian Date given the inputs of a day, month and whether the year is a leap year. The day and month inputs come from two Combo Boxes which produce String Values for the name of the month, and the Date. I then have a Checkbox to state whether or not the year is a leap year.

In calculating a Julian Date, I make a MM/DD or DD/MM date a value between 1 and 365 or 1 and 366 in a leap year. In such January 1st is 1 and December 31st is 365. My design was to use string arrays with the corresponding Julian Days for each month. So if I chose January and a corresponding date, the date part is used to find the position in the array. To do this, I created a class that carried the corresponding Julian Days for each month in a string Array.

The Month class and method is called when the text representing the month is found in some conditional statements after we have started the result event. After we have the array available, we use the date text to find the position in the Array.

The other major design area was the Leap Year as the majority of days have different values. The check box denoted it was a leap year but the first 59 days have the same value. For February the 29th, I made a specific piece of code to create the Julian day of 60, and all days after I used the some logic to add one the Julian Date created on 365 day year.

The Form

To make the program factor a leap year, I use a counter of sorts as it will be 0 if the Leap Year CheckBox is unchecked, and this value of zero is used to denote do not factor for a leap year. Corresponding to this, the 1 denotes a leap year. There is also the code to fill the Combo Boxes with the correct Text.

//The leap Year variable is the first thing I do with my code

public partial class JulianClaendar : Form
    {
        int leapyear = 0;
        public JulianClaendar()
        {
         // apps code
        }

       private void chckBxLeapYear_CheckedChanged(object sender, EventArgs e)
        {
            leapyear = 1;
        }
    }

Result Event

The first action is to create an empty string array for the Month class methods to fill after some basic date validation is done.

The date validation effects the months that do not have 31 days in them as my Date ComboBox goes to 31.

After the figuring of the non dates, I do as stated above and have some conditional code to figure out which month it is, then the correct method is chosen from the Month class. These conditions are checked for each month.

The Class gives me the string Array of Julian Dates for that month. I need to get the correct position for that date so I convert the text from the Date ComboBox to an Integer, then I minus one from it and that gives the text for the corresponding Julian Date. This text is then placed in the Label for the Result.

//
private void button1_Click(object sender, EventArgs e)
        {
         string [] MONTHRESULT;
         
// Feb 29th inclusion date for leap year            
         if (chckBxLeapYear.Checked = true && 
         comboBoxMonth.Text == "February" 
             && comboBoxDay.Text == 
             "30" || (chckBxLeapYear.Checked = true 
             && comboBoxMonth.Text == 
             "February" && comboBoxDay.Text == "31"))
            {
              
                MessageBox.Show("This dates do not exist", "Warning");
                return;
            }
// exclusion of the non dates          

           if ( leapyear == 0 && comboBoxMonth.Text == 
           "February" && comboBoxDay.Text == "29" 
                || leapyear == 0 && comboBoxMonth.Text == 
                "February" && comboBoxDay.Text == "30"
                || leapyear == 0 && comboBoxMonth.Text == 
                "February" && comboBoxDay.Text == "31")
            {
             MessageBox.Show("This dates do not exist", "Warning");
             return;
            }
          // remainder of code for event

         Month ChosenMonth = new Month();
         string day = comboBoxDay.Text;
         int arraypos;
         string Juledate;

         if (comboBoxMonth.Text == "January")
            {
                MONTHRESULT = ChosenMonth.jan();
                arraypos = Convert.ToInt32(day) - 1;
                Juledate = MONTHRESULT[arraypos];
                lblResult.Text = Juledate;
            }             
       }   

Month Class

The class as stated before is 12 methods that return the Julian Date values for each month in the year. I have included only January and May as to make examples, as January's Julian Dates are the same as the date within the month, so I chose May to illustrate how the methods returned different values within the arrays.

 class Month
    public string[] Jan()
    {
      
      string[] result = new string[] { "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"
                                     }; 
     return result;
  }

// remainder of methods

public string[] May()
        {
            string[] result = new string[] { "152", "153", "154", "155", "156", "157", 
                                             "158", "159", "160","161", "162", "163" , 
                                             "164", "165", "166", "167", "168", "169",
                                             "170", "171", "172", "173", "174", "175", 
                                             "176", "177", "178", "179", "180", "181"
                                           };?
           return result;
        }

Leap Year and February 29th

To calculate February 29th conditions had to be met that is month day and leap year; if they are met, we make the result label 60 and skip the remainder of the conditional code. If it is February 29th on a leap year, the Logic just returns a string of 60 and exits the event at this point.

As stated above, we also had to figure out all the Julian Dates for the dates after February 29th. This is done by the last piece of conditional code in the event and it checks if it is a leap year and whether the date on 365 day year is equal to or greater than 60 (March 1st). As the 365 date was created and placed in the Result Label, we then use this string to make an integer, add one to it and then cast it as the string for the Result Label.

 if (comboBoxMonth.Text == "February" )
            {
                if (leapyear == 1 && comboBoxDay.Text == "29")
                {

                  blResult.Text = "60";
                    leapyear = 0;
                    return;
                }
                else
                {
                    MONTHRESULT = ChosenMonth.feb();
                    arraypos = Convert.ToInt32(day) - 1;
                    Juledate = MONTHRESULT[arraypos];
                    lblResult.Text = Juledate;
                }
             }

// remainder of code

            if (leapyear ==1)
            {
                int juliandate = Convert.ToInt32(lblResult.Text);
                if (juliandate >= 60)
                {
                    juliandate = juliandate + 1;
                    lblResult.Text = juliandate.ToString();
                }
            }

            leapyear = 0;

Points of Interest

Well, this exercise got me to think of how you need to make sure you do the events at the correct time. It also got me thinking about how best to accomplish a small task, as I found one class of 12 arrays accomplished it, when you took your input from a few form objects. I did add a second download with a Class Library and called that, so if one wanted to make the program with a class library, then the option to view that solution is there too!

History

  • 15th October, 2015: Initial version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here