How does one convert a Month or Day to reflect as a 2 digit string using C#
For example : (02 instead of 2)
How does one convert a Month or Day to reflect as a 2 digit string using C#
For example : (02 instead of 2)
If you have a DateTime
, use its string formatters:
string month = DateTime.Now.ToString("MM"); // or "dd" for day
Or if it makes more sense to work with a number that you have, use the numeric formatters:
string monthStr = monthInt.ToString("00");
Use "MM"
for double digits month and "dd"
for double digits days.
string month = DateTime.Now.ToString("MM");
DateTime.Now.Day.ToString().PadLeft(2, '0');