Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I keep the 0's in a Date

Tags:

c#

I am trying to figure out how it is that I can keep the 0's or add them when I grab a date.

What Im getting is this:

6/15/2010

What I'm tring to get is:

06/15/2010

I have added it so that it checks the length to and if its less than 6 (im stripping the "/") it pads the left side. That solves the issue when the month is a single digit, but what about when the date is a single digit.

My ultimate goal is to have a date such as:

1/1/2010

read out like:

01/01/2010

Any suggestions would be greatly appreciated.

like image 536
Scott's Oasys Avatar asked Jan 21 '23 21:01

Scott's Oasys


1 Answers

Use a custom format : dd/MM/yyyy, or in your case MM/dd/yyyy. Note the capital M, the small m gets you the minutes.

string s = DateTime.Now.ToString("MM/dd/yyyy");
like image 102
Henk Holterman Avatar answered Jan 24 '23 12:01

Henk Holterman