Member-only story
DateTime in C#
In C#, the DateTime struct is a fundamental data type used for working with dates and times. It is part of the System namespace and provides a wide range of methods and properties for handling date and time information
Features with datetime in C#:
Creating a DateTime Object:
You can create a DateTime object to represent a specific date and time. There are several constructors available, but the most common one takes year, month, day, hour, minute, and second as parameters:
Example
using System;
DateTime dateTime = new DateTime(2023, 10, 31, 14, 30, 0);
Current Date and Time:
To get the current date and time, you can use the DateTime.Now property:
Example
DateTime currentDateTime = DateTime.Now;
Date and Time Formatting:
You can format a DateTime object into a string using the ToString method or specifying a format string:
Example
string formattedDateTime = currentDateTime.ToString("yyyy-MM-dd HH:mm:ss");