开发者

C#: Change format Day with Leading zero In DateTime

开发者 https://www.devze.com 2022-12-25 15:36 出处:网络
in C#, DateTime.Day.Now.ToString() is returning 1.开发者_开发技巧 How can i get it as 01?Two quick methods

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")

0

精彩评论

暂无评论...
验证码 换一张
取 消