2009-07-20 39 views

回答

95
DateTime.Now.Month.ToString("d2") 
45

两种格式用两位数字的整数通过迈赫达德的建议,或格式化DateTime本身给你一个两位数的月份:

DateTime.Now.ToString("MM") 
+0

是它的工作.Thanks的迈赫达德乔恩 – Shyju 2009-07-20 09:34:49

10

谢谢,正是我需要的。我使用webdriver的硒来测试网站,并在一开始设置各种瓦尔/串等,这使得它简单了很多,现在我使用的代码是:

/* Date strings */ 
    public static string TodayNum = DateTime.Now.ToString("dd");  // e.g 07 
    public static string ThisMonthNum = DateTime.Now.ToString("MM"); // e.g 03 
    public static string ThisMonthShort = DateTime.Now.ToString("MMM"); // e.g Mar 
    public static string ThisMonthLong = DateTime.Now.ToString("MMMM"); // e.g March 
    public static string ThisYearNum = DateTime.Now.ToString("yyyy"); // e.g 2014 
4

你可以使用:DateTime.Now.Month.ToString().PadLeft(2, '0');

它将返回:07

2

您还可以将月和日,以两位数字的以下功能太:

System.DateTime.Now.Month.ToString("00") //Give 01 for January 
System.DateTime.Now.Day.ToString("00")