AllTheTimeWorld.com

csharp objectsWorking with the DateTime class.

Working with the DateTime class.

Working with the DateTime class.

We will often have a date as part of another class. The DateTime class includes both a date and time.

Please study the code below and add it to form load one line at a time.

DateTime today = System.DateTime.Now;
this.Text = today.ToString(); // Example: 4/19/2018 10:58:32 AM
this.Text = "" + today.TimeOfDay; // Example: 10:58:32.1336708
this.Text = "" + today.ToShortDateString(); // Example: 4/19/2018
this.Text = "" + today.Month; // Example: 4
this.Text = "" + today.DayOfWeek; // Example: Thursday
this.Text = "" + today.Year; // Example: 2018
// The due date has just the month day and year, but not the time.
// When we compare the due date to today we compare just the date part.
DateTime dueDate = new DateTime(2018, 5, 21); //y,m,d
if (dueDate.Date < today.Date)
this.Text = "You are late";
else if (dueDate.Date == today.Date)
this.Text = "It is due today";
else this.Text = "You still have time";