
C# declare datetime variable, its range and type conversion tostring
1. Datetime range
Datetime is used to declare date variables, its "namespace" is "System", and the value range is 0001-01-01 00:00:00.0000000 to 9999-12-31 23:59:59.9999999.
2. C# declare datetime variable
DateTime dt = DateTime.Now;
DateTime dt = new DateTime(2020, 8, 23, 16, 59, 18);
DateTime dt1, dt2;//The default value is 0001-1-1
DateTime dt1 = DateTime.Now, dt2 = new DateTime(2020, 8, 24);
DateTime[] dtArr = {DateTime.Now, new DateTime(2020, 8, 24)};//Date array
3. C# datetime tostring(Convert datetime to string c#)
DateTime d = DateTime.Now;
string day = d.ToString();
string day = Convert.ToString(d);
4. C# datetime tostring(Output format)
DateTime d = DateTime.Now;
string day = d.ToString("yyyy-MM-dd");//Output month, day and year, such as 8/24/2020
string hms = d.ToString("yyyy-MM-dd hh:mm:ss");//Output month, day, year, hour, minute, and second, such as 08/24/2020 05:13:56
string day = d.ToShortDateString();//Output month, day and year, such as 8/24/2020
string hm = d.ToShortTimeString();//Output hour and minute, such as 5:17 PM
string day = d.ToLocalTime().ToString();//Converted to local time, such as 8/24/2020 5:18:14 PM
string day = d.ToLongDateString();//Output month, day and year and week, such as Monday, August 24, 2020
string hms = d.ToLongTimeString();//Output hour, minute and second, such as 5:19:49 PM