Working With Dates
DateTime
DateTimeIn the System namespace, we have access to the DateTime class.
var dateTime = new DateTime(2020, 09, 28); // year, month, day
var now = DateTime.Now;
var today = DateTime.Today;
Console.WriteLine("Hour: ", now.Hour);Note: DateTime objects are immutable. You can, however, increment/decrement time with them using any AddX methods that return a new DateTime object:
var tomorrow = now.AddDays(1);
var yesterday = now.AddDays(-1);You can also convert DateTime objects to strings:
now.ToLongDateString();
now.ToShortDateString();
now.ToLongTimeString();
now.ToShortTimeString();
now.ToString("yyyy-MM-dd HH:mm"); // date and time WITH format specifierTimeSpan
TimeSpanTimeSpan is where you can represent a length of time.
Once again, TimeSpan is immutable, but it has useful methods for updating that return a new TimeSpan object:
Conversion to and from strings:
Last updated