在此C#代码段中,DateTime.Now.Month.ToString()返回7输出.
我想得到07一个返回值.
当月份只有1位数时,我该怎么做才能添加前导零?
Jon*_*eet 51
根据Mehrdad的建议格式化两位数的整数,或者格式化DateTime自身以给出两位数的月份:
DateTime.Now.ToString("MM")
Run Code Online (Sandbox Code Playgroud)
小智 17
谢谢,正是我需要的.我正在使用Selenium 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
Run Code Online (Sandbox Code Playgroud)
您还可以通过以下函数将月份和日期转换为两位数:
System.DateTime.Now.Month.ToString("00") //Give 01 for January
System.DateTime.Now.Day.ToString("00")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
82958 次 |
| 最近记录: |