C#實現的一些常見時間格式
阿新 • • 發佈:2020-04-22
string aa = DateTime.Now.ToShortDateString();//"2019/9/23" string bb = DateTime.Now.ToShortTimeString();//"上午 10:21" string ff = DateTime.Now.ToLongDateString();//2019年9月23日 string gg = DateTime.Now.ToLongTimeString();//上午 10:22:07
int monthDays = DateTime.DaysInMonth(DateTime.Now.Year,DateTime.Now.Month);//獲取某月在某年中的天數DateTime From = DateTime.ParseExact(/*tdFrom.Text*/"201909","yyyyMM",DateTimeFormatInfo.InvariantInfo);//輸入的String轉Datetime DateTime End = DateTime.ParseExact(/*tdEnd.Text*/"201909","yyyyMM",DateTimeFormatInfo.InvariantInfo); string nian = DateTime.Now.ToString("Y");//2019年9月 string nian1 = DateTime.Now.ToString("y");//2019年9月 string y1 = DateTime.Now.ToString("R");//Mon,23 Sep 2019 10:31:48 GMT string y2 = DateTime.Now.ToString("r");//Mon,23 Sep 2019 10:32:10 GMT string xingqiji = DateTime.Now.ToString("dddd");// "星期一" string zhouji = DateTime.Now.ToString("ddd");// "週一" string ri = DateTime.Now.ToString("dd");// 23 stringershisi = DateTime.Now.ToString("HH"); //3 string shier = DateTime.Now.ToString("hh");//15 string chang = DateTime.Now.ToString("F");//2019年9月23日 上午 10:34:08 string chang1 = DateTime.Now.ToString("f");//2019年9月23日 上午 10:34 string cahng2 = DateTime.Now.ToString("G");//2019/9/23 上午 10:35:07 string cahng3 = DateTime.Now.ToString("g");//2019/9/23 上午 10:35 string cahng4 = DateTime.Now.ToString("D");//2019年9月23日 string cahng5 = DateTime.Now.ToString("d");//2019/9/23 string now = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");//2019/09/23 10:14:54 string a = DateTime.Now.AddYears(20).ToString("yyyy/MM/dd HH:mm:ss");//2039/09/23 10:14:54 string b = DateTime.Now.AddMonths(-2).ToString("yyyy/MM/dd HH:mm:ss");//"2019/07/23 10:14:54" string c = DateTime.Now.AddDays(-8).ToString("yyyy/MM/dd HH:mm:ss");//"2019/09/15 10:14:54" string d = DateTime.Now.AddHours(24).ToString("yyyy/MM/dd HH:mm:ss");//"2019/09/24 10:14:54" string f = DateTime.Now.AddMinutes(1440).ToString("yyyy/MM/dd HH:mm:ss");//2019/09/24 10:14:54 string g = DateTime.Now.AddSeconds(3600).ToString("yyyy/MM/dd HH:mm:ss");//2019/09/23 11:14:54" string h = DateTime.Now.AddSeconds(3600).ToString("yyyy/MM/dd HH:mm:ss:ffff");//2019/09/23 11:14:54:7992