Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make convert a Month or Day to reflect as a 2 digit string using C#

Tags:

c#

How does one convert a Month or Day to reflect as a 2 digit string using C#

For example : (02 instead of 2)

like image 330
Pinch Avatar asked Oct 07 '13 19:10

Pinch


People also ask

How do I get month and date of Javascript in 2 digit format?

Use the getMonth() method to get the month for the given date. Use the getDate() method to get the day of the month for the given date. Use the padStart() method to get the values in a 2-digit format.

What is FFF in time format?

The "fff" custom format specifier represents the three most significant digits of the seconds fraction; that is, it represents the milliseconds in a date and time value.


1 Answers

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"); 
like image 72
Tim S. Avatar answered Sep 22 '22 10:09

Tim S.