C# DateTime ToString("MM-dd-yyyy") 返回有趣的日期值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1411339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# DateTime ToString("MM-dd-yyyy") returns funny day values
提问by Harper Shelby
I have the following code in the codebehind file of an ASP.Net page
我在 ASP.Net 页面的代码隐藏文件中有以下代码
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?
我希望返回“09-11-2009”。但是,当我在开发服务器上运行该页面时,我在文本框中看到“09-00-2009”。我看不出有任何原因,所以我显然错过了一些东西。有人有线索吗?
采纳答案by Scott Ivey
That format stringshould 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.
该格式字符串应该按预期工作。我会检查您的文本框以确保您没有某种掩码(AJAX MaskedEditExtender?)。如果您这样做了,并且可能蒙版不正确,它可能会覆盖您放入文本框中的内容。
回答by Marc Gravell
I can't think why it would show 00, but as a random suggestion you could try:
我想不通为什么会显示 00,但作为随机建议,您可以尝试:
... = DateTime.Today.ToString("MM-dd-yyyy", CultureInfo.InvariantCulture);
回答by Khodor
could you check Datetime,Now.ToString() ?
你能检查 Datetime,Now.ToString() 吗?
回答by Nirlep
Try something like this:
尝试这样的事情:
DateTime.Today.ToString("MM-dd-yyyy", CultureInfo.CreateSpecificCulture("en-US"))
回答by Michael Olesen
Use .Now to get the local time
使用 .Now 获取本地时间
txtStartDate.Text = DateTime.Now.ToString("MM-dd-yyyy");