Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# DateTime ToString("MM-dd-yyyy") returns funny day values

I have the following code in the codebehind file of an ASP.Net page

txtStartDate.Text = DateTime.Today.ToString("MM-dd-yyyy");

Which I expect to return "09-11-2009". However, when I run the page on the development server, I see "09-00-2009" in the text box. I can't see any reason for this, so I'm clearly missing something. Anyone have a clue?

like image 817
Harper Shelby Avatar asked Sep 11 '09 14:09

Harper Shelby


2 Answers

I can't think why it would show 00, but as a random suggestion you could try:

... = DateTime.Today.ToString("MM-dd-yyyy", CultureInfo.InvariantCulture);
like image 184
Marc Gravell Avatar answered Sep 23 '22 01:09

Marc Gravell


That format string should work as expected. I'd check your textbox to make sure you don't have some sort of mask (AJAX MaskedEditExtender?) on it. If you did, and maybe had the mask incorrect, it could overwrite what you were putting in the textbox.

like image 40
Scott Ivey Avatar answered Sep 22 '22 01:09

Scott Ivey