Friday, May 9, 2014

More On Date / Time


namespace More_On_DateTime
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCompareDateTime_Click(object sender, EventArgs e)
        {
            //use the > operator to compare two date
            //get user input 
            DateTime dt1 = dateTimePicker1.Value;
            DateTime dt2 = dateTimePicker2.Value;

            //compare >, <,  ==, 
            if (dt1 > dt2)
            {
                richTextBox1.Text = String.Format("{0} is most recent", dt1);
            }
            else if (dt1 < dt2)
            {
                richTextBox1.Text = String.Format("{0} is most recent", dt2);
            }
            else //must be equal
            {
                richTextBox1.Text = "Both times are equal";
            }
        }

        private void btnTimeDiff_Click(object sender, EventArgs e)
        {
            TimeSpan ts;
            //get user input 
            DateTime dt1 = dateTimePicker1.Value;
            DateTime dt2 = dateTimePicker2.Value;

            if (dt1 > dt2)
                ts = dt1 - dt2;
            else
                ts = dt2 - dt1;
           
            //Display timespan properties

            richTextBox1.Text = "Days: " + ts.Days + "\n" +
                                "Hours: " + ts.Hours + "\n" +
                                "Minutes: " + ts.Minutes + "\n" +
                                "Seconds: " + ts.Seconds + "\n\n" +
                                "TotalDays: " + ts.TotalDays + "\n" +
                                "TotalHours: " + ts.TotalHours;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //in order to work the application update
            //set maximum date inside the form load

            dateTimePicker3.MaxDate = DateTime.Now;

            //Set the min to 150 yr back from current date
            dateTimePicker3.MinDate = DateTime.Now.AddYears(-150);

        }

        private void btnGetAge_Click(object sender, EventArgs e)
        {
            TimeSpan dt = DateTime.Now - dateTimePicker3.Value;
            int years = dt.Days / 365;
            if (years > 2)
                MessageBox.Show(String.Format(
                   "you are {0} years old", years));
            else
            {
                int months = (dt.Days % 365) / 30;
                months += 12 * years;
                MessageBox.Show(String.Format(
                    "you are {0} months old", months));
            }
        }
    }
}
//To read: click here

///Lab Assignment: Add to the Person class
///Add method GetAge() to the Person class
///this method returns the numbers of years old
///
///Add method GetDaysTillBirthDay()
///this method should return the number of days 
///left until the person's birthday
///
///in form1: re-adjust the listview by adding 2 more colums: "Age", and "Days" left to brithdate
///
///Display the List<Person> with the additional information.
///
///In form1 add a method GetYoungest() that returns
///a person (the youngest person)
///add method GetOldest() that returns the oldest person
///Add button to call these methods and display.

No comments:

Post a Comment