Thursday, May 8, 2014

The Date Time Structure


namespace The_DateTime_Structure
{

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

        private void btnCurrentDateTime_Click(object sender, EventArgs e)
        {
            ///Use the DateTime property 'Now' to get the current
            ///date and time. The 'Now' property is of type DateTime
            DateTime currentDT = DateTime.Now;
            //get the current year
            int year = currentDT.Year;
            int month = currentDT.Month;
            int dayofyear = currentDT.DayOfYear;
            int dayofmonth = currentDT.Day;
            DayOfWeek dayofweek = currentDT.DayOfWeek;
            int hour = currentDT.Hour;
            int minute = currentDT.Minute;

            richTextBox1.Text =
                "year: " + year + "\n" +
                "month: " + month + "\n" +
                "day of year: " + dayofyear + "\n" +
                "day of month: " + dayofmonth + "\n" +
                "day of week: " + dayofweek + "\n" +
                "hour: " + hour + "\n" +
                "minute: " + minute;
        }

        private void btnDisplayDateTime_Click(object sender, EventArgs e)
        {
            //display the current datetime using the methods
            //defined in the DateTime structure
            DateTime currentDT = DateTime.Now;
            richTextBox1.Text =
                "ToString:  " + currentDT.ToString() + "\n" +
                "ToLongDateString: " + currentDT.ToLongDateString() + "\n" +
                "ToShortDateString:  " + currentDT.ToShortDateString() + "\n" +
                "ToLongTimeString:  " + currentDT.ToLongTimeString() + "\n" +
                "ToShortTimeString:  " + currentDT.ToShortTimeString();
        }
        //method to display a DateTime object
        private void DisplayDateTime(DateTime dt)
        {
            richTextBox1.AppendText(
               "ToString:  " + dt.ToString() + "\n" +
               "ToLongDateString: " + dt.ToLongDateString() + "\n" +
               "ToShortDateString:  " + dt.ToShortDateString() + "\n" +
               "ToLongTimeString:  " + dt.ToLongTimeString() + "\n" +
               "ToShortTimeString:  " + dt.ToShortTimeString()+
               "\n\n");
        }

        private void btnCreateDateTime_Click(object sender, EventArgs e)
        {
            //Create a DateTime object without including Time
            DateTime dt1 = new DateTime(1981, 2, 15);
            //since time did not get set in the
            //constructor, it is by default
            //midnight: hour:00 min:00 sec:00)

            //Create a DateTime with Time included
            DateTime dt2 = new DateTime(2014, 5, 10, 15, 30, 0);

            DisplayDateTime(dt1);
            DisplayDateTime(dt2);
        }

        private void btnCreateDTfromDT_Click(object sender, EventArgs e)
        {
            //to create a DateTime from another 
            //DateTime use the AddXXX methods

            //you want to create a DateTime
            //for a date that is 10 years from now
            DateTime currentDT = DateTime.Now;
            //create a new DateTime from the 
            //current one (10 years later)
            DateTime laterDT = currentDT.AddYears(10);
            DisplayDateTime(laterDT);
        }

        private void btnUserSelectedDate_Click(object sender, EventArgs e)
        {
            DateTime selectedDT =
                dateTimePicker1.Value;
            //display it
            DisplayDateTime(selectedDT);
        }
    }
}

No comments:

Post a Comment