views:

1225

answers:

4

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?

+6  A: 

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);
Marc Gravell
+3  A: 

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.

Scott Ivey
It's got the AJAX CalendarExtender on it, but the format string for both is identical.
Harper Shelby
That was it - I had made an error in copying the format string for the extender.
Harper Shelby
A: 

could you check Datetime,Now.ToString() ?

Kheu
Yeah, I checked that - same result.
Harper Shelby
+1  A: 

Try something like this:

DateTime.Today.ToString("MM-dd-yyyy", CultureInfo.CreateSpecificCulture("en-US"))
Neil