Working with System.DateTime – Date and Time String Formatting with C#
Software Development with C# needs to work with dates and times. To work efficiently with Date and Time .NET has the System.DateTime (DateTime) namespace to help us. You can use DateTime to get DateTime values, generate new DateTime values and format DateTime values (parse strings into DateTime or DateTime in the string format).
Creating New DateTime Objects with C#
The default value of DateTime is Jan 1, 0001 at 12:00 midnight.
DateTime newDate = new DateTime();
Console.WriteLine("date: {0}", newDate); //date: 1/1/0001 12:00:00 AM
Creating New DateTime through the constructor
You can initialize the DateTime through the constructor using several pre-define options.
System.DateTime newDate = new System.DateTime(2010, 9, 5, 21, 35, 15, 777);
Console.WriteLine("date: {0}", newDate);//date: 9/5/2010 9:35:15 PM
If you need the current date and time? DateTime.Now will do it for you.
System.DateTime newDate = System.DateTime.Now;
Console.WriteLine("date: {0}", newDate);// Current Date Time will be display
Date and Time String Formatting with C#
The date and time value can be represented by either a DateTime or a DateTimeOffset value. Date and Time String Formatting fall into two categories:
- Standard Date and Time String Formatting
- Custom Date and Time String Formatting




