As you might have guessed, System.DateTime is an ASP.NET value type that represents dates and times. The datetime structures values can range from January 1st 0001 12:00 to December 31st 9999 11:59:59 PM (looks like a year 10 000 bug in the works!).
As for the time values, they are measured in 100-nanosecond unites which are also commonly known as 'ticks'.
The DateTime object will by default use the Gregorian calendar unless you explicitly pass a IFormatProvider parameter stating otherwise.
Some common methods of the DateTime structure are: Add, AddDays, AddHours, AddMinutes, AddMonths, AddSeconds, AddYears, DaysInMonth along with the various permuations of the ToString() overload for DateTime.
If you want to initialize the DateTime object with a value (which you have to do i.e. it cannot be null ofcourse) you can use DateTime.MinValue or DateTime.MaxValue.
For reference purposes I'm going to show you some common datetime formats below, this reference will come in handy when you are developing trust me!
Examples using DateTime in ASP.NET
Instead of explaining many of the common uses of the datetime structure, I've created an .aspx page that contains many of the common uses. I've included both the code and output for your viewing pleasure.
What you'll find in the examples:
- How to display the date in various formats using ToString().
- How to get the number of days in a month.
- How to add or subtract minutes, seconds, hours, days, months or even years from a date.
- How to compare two different datetimes.
Live Demo of DateTime Structure examples
Follow the link to view the examples, I will keep this example updated as I get more code snippets.