格式化日期字符串的自定义方法
1 /// <summary> 2 /// 根据系统日期,格式化日期字符串,返回的格式为20100101; 3 /// </summary> 4 /// <returns>返回格式化号的字符串</returns> 5 public static string getday() 6 { 7 string stryear, strmonth, strday; 8 //获取年份的字符串 9 stryear = DateTime.Now.Year.ToString(); 10 //获取当天日期的字符串,格式是“00” 11 if (DateTime.Now.Day.ToString().Length != 2) 12 { 13 strday = "0" + DateTime.Now.Day.ToString(); 14 } 15 else 16 { 17 strday = DateTime.Now.Day.ToString(); 18 } 19 //获取月份的字符串,格式是“00”月 20 if (DateTime.Now.Month.ToString().Length != 2) 21 { 22 strmonth = "0" + DateTime.Now.Month.ToString(); 23 } 24 else 25 { 26 strmonth = DateTime.Now.Month.ToString(); 27 } 28 string pcode = stryear + strmonth + strday; 29 return pcode; 30 }