in C#,
DateTime.Day.Now.ToString()
is returning 1
.开发者_开发技巧 How can i get it as 01
?
Two quick methods
DateTime.Now.Day.ToString("00");
DateTime.Now.ToString("dd");
Try this:
DateTime.Now.Day.ToString("00");
DateTime.Now.ToString("MM/dd/yyyy")
edit: this assumes the questioner wanted the full date formatted with a double-digit day. If he wanted the day only, then other answers are better.
DateTime.Now.Day.ToString("D2")
This is an example of how to format a date time in C#
You can use different formatting options behind the \ to make the date appear however you want.
DateTime currentDate = DateTime.Now;
// use the current date/time to configure the output directory
String dateTime = String.Format( "{0:yyyy-MM-dd}", currentDate);
if(DateTime.Now.Day < 10)
return "0" + DateTime.Now.Day.ToString();
else return DateTime.Now.Day.ToString();
Try using
DateTime.Now.ToString("dd");
use format specifier
$"{today.Year}{today.Month:00}{today.Day:00}";
This works universally for any number: myNumber.ToString("00")
精彩评论