格式化日期字符串的自定义方法

 1 /// 
 2     /// 根据系统日期,格式化日期字符串,返回的格式为20100101;
 3     /// 
 4     /// 返回格式化号的字符串
 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     }

 

转载于:https://www.cnblogs.com/kdkler/p/4680518.html

你可能感兴趣的:(格式化日期字符串的自定义方法)