SoFunction
Updated on 2025-03-06

Analysis of interconversion operation examples of C# date format strings

This article describes the interconversion operation of C# date format strings. Share it for your reference, as follows:

Method 1: (string)

There are requirements for string format, it must be yyyy-MM-dd hh:mm:ss

Method 2: (string, IFormatProvider)

DateTime dt;
DateTimeFormatInfo dtFormat = new ();
 = "yyyy/MM/dd";
dt = ("2011/05/26", dtFormat);

Method 3: ()

string dateString = "20110526";

DateTime dt = (dateString, "yyyyMMdd", );

or

DateTime dt = (dateString, "yyyyMMdd", );

Attached reference information:

CultureInfo cultureInfo = ("en-US");
string format = "ddd MMM d HH:mm:ss zz00 yyyy";
string stringValue = (format, cultureInfo); // Get the date stringDateTime datetime = ("Wed Aug 25 16:28:03 +0800 2010", format, cultureInfo); // Convert string to date

Other related:

Date format: yyyyMMdd HH:mm:ss (note that the letter case of this string is very strict)

yyyy: Represents the year
MM: Represents the month
dd: Representative day
HH: Representative hours (24-hour system)
mm: Represents minutes
ss: Represents seconds
()
DateTime dt = ;
();//2005-11-5 13:21:25
().ToString();//127756416859912816
().ToString();//127756704859912816
().ToString();//2005-11-5 21:21:25
().ToString();// November 5, 2005().ToString();//13:21:25
().ToString();//38661.5565508218
().ToString();//2005-11-5
().ToString();//13:21
().ToString();//2005-11-5 5:21:25
();//2005
();//2005-11-5 0:00:00
();//Saturday
();//309
();//13
();//441
();//30
();//11
();//28
();//632667942284412864
();//13:30:28.4412864
();//2005-11-5 13:47:04
(1).ToString();//2006-11-5 13:47:04
(1.1).ToString();//2005-11-6 16:11:04
(1.1).ToString();//2005-11-5 14:53:04
(1.1).ToString();//2005-11-5 13:47:04
(1).ToString();//2005-12-5 13:47:04
(1.1).ToString();//2005-11-5 13:47:05
(1.1).ToString();//2005-11-5 13:48:10
(1000).ToString();//2005-11-5 13:47:04
(dt).ToString();//0
(?).ToString();//The question mark is a time period("2005-11-6 16:11:04").ToString();//False
(dt).ToString();//True
().ToString();//1474088234
().ToString();//
().ToString();//DateTime
('s')[0].ToString();//2005-11-05T14:06:25
('t')[0].ToString();//14:06
('y')[0].ToString();//November 2005('D')[0].ToString();// November 5, 2005('D')[1].ToString();//2005 11 05
('D')[2].ToString();//Saturday 2005 11 05('D')[3].ToString();//Saturday November 5, 2005('M')[0].ToString();//November 5('f')[0].ToString();//November 5, 2005 14:06('g')[0].ToString();//2005-11-5 14:06
('r')[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT
("{0:d}",dt);//2005-11-5
("{0}",dt);// November 5, 2005("{0:f}",dt);//November 5, 2005 14:23("{0:F}",dt);//November 5, 2005 14:23:23("{0:g}",dt);//2005-11-5 14:23
("{0:G}",dt);//2005-11-5 14:23:23
("{0:M}",dt);//November 5("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT
("{0:s}",dt);//2005-11-05T14:23:23
("{0:t}",dt);//14:23
("{0:T}",dt);//14:23:23
("{0:u}",dt);//2005-11-05 14:23:23Z
("{0:U}",dt);//November 5, 2005 6:23:23("{0:Y}",dt);//November 2005("{0}",dt);//2005-11-5 14:23:23
("{0:yyyyMMddHHmmssffff}",dt);

Calculate the difference in the number of days between 2 dates

DateTime dt1 = ("2007-8-1");
DateTime dt2 = ("2007-8-15");
TimeSpan span = (dt1);
int dayDiff =  + 1;

Calculate the number of days in a certain year and month

int days = (2007, 8);
days = 31;

Add one day to a date and reduce one day

DateTime dt =;
(1); //Add a day(-1);//Reduce one day

Other years are similar...

Convert date function in Oracle SQL

to_date("2007-6-6",'YYYY-MM-DD");
to_date("2007/6/6",'yyyy/mm/dd");

PS: Here are a few date and time-related tools for your reference:

In the date day difference calculator:
http://tools./jisuanqi/onlinedatejsq

Online date calculator/phase difference day calculator:
http://tools./jisuanqi/datecalc

Online Date/Day Calculator:
http://tools./jisuanqi/date_jisuanqi

Online Lunar/Gregorian calendar conversion tool:
http://tools./bianmin/yinli2yangli

Online Day Calculator (flash version):
http://tools./jisuanqi/datejsq

For more content related to C#, please view the special topic of this site: "Summary of C# date and time operation skills》、《Summary of C# string operation skills》、《Summary of C# array operation skills》、《Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial"and"Introduction to C# object-oriented programming tutorial

I hope this article will be helpful to everyone's C# programming.