在这个C#代码片段中,DateTime.Now.Month.ToString()
返回7
作为输出。C#:DateTime.Now月份输出格式
我想获得07
作为返回值。
当月份只有1位数字时,如何添加前导零?
在这个C#代码片段中,DateTime.Now.Month.ToString()
返回7
作为输出。C#:DateTime.Now月份输出格式
我想获得07
作为返回值。
当月份只有1位数字时,如何添加前导零?
DateTime.Now.Month.ToString("d2")
两种格式用两位数字的整数通过迈赫达德的建议,或格式化DateTime
本身给你一个两位数的月份:
DateTime.Now.ToString("MM")
谢谢,正是我需要的。我使用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
你可以使用:DateTime.Now.Month.ToString().PadLeft(2, '0');
它将返回:07
您还可以将月和日,以两位数字的以下功能太:
System.DateTime.Now.Month.ToString("00") //Give 01 for January
System.DateTime.Now.Day.ToString("00")
是它的工作.Thanks的迈赫达德乔恩 – Shyju 2009-07-20 09:34:49