SoFunction
Updated on 2025-04-10

C# implements common time formats

Sometimes we have to format the time to achieve different display effects. This article has compiled some common time format implementation methods in C# for you.

string aa = ();//"2019/9/23"
string bb = ();//"10:21 am"string ff = ();//September 23, 2019string gg = ();//10:22:07 am
int monthDays = (, );//Get the number of days in a certain month in a certain year
DateTime From = (/**/"201909", "yyyyMM", );//Input String to Datetime
 DateTime End = (/**/"201909", "yyyyMM", );

string nian = ("Y");//September 2019string nian1 = ("y");//September 2019
string y1 = ("R");//Mon, 23 Sep 2019 10:31:48 GMT
string y2 = ("r");//Mon, 23 Sep 2019 10:32:10 GMT

string xingqiji = ("dddd");// "Monday"string zhouji = ("ddd");//  "on Monday"string ri = ("dd");// 23
string ershisi = ("HH"); //3
string shier = ("hh");//15

string chang = ("F");//September 23, 2019 10:34:08 amstring chang1 = ("f");//September 23, 2019 at 10:34 amstring cahng2 = ("G");//2019/9/23 10:35:07 amstring cahng3 = ("g");//2019/9/23 10:35 amstring cahng4 = ("D");//September 23, 2019string cahng5 = ("d");//2019/9/23

string now = ("yyyy/MM/dd HH:mm:ss");//2019/09/23 10:14:54
string a = (20).ToString("yyyy/MM/dd HH:mm:ss");//2039/09/23 10:14:54
string b = (-2).ToString("yyyy/MM/dd HH:mm:ss");//"2019/07/23 10:14:54"
string c = (-8).ToString("yyyy/MM/dd HH:mm:ss");//"2019/09/15 10:14:54"
string d = (24).ToString("yyyy/MM/dd HH:mm:ss");//"2019/09/24 10:14:54"
string f = (1440).ToString("yyyy/MM/dd HH:mm:ss");//2019/09/24 10:14:54
string g = (3600).ToString("yyyy/MM/dd HH:mm:ss");//2019/09/23 11:14:54"
string h = (3600).ToString("yyyy/MM/dd HH:mm:ss:ffff");//2019/09/23 11:14:54:7992

This is all about this article about implementing common time formats in C#. I hope it will be helpful to everyone's learning and I hope everyone will support me more.