如何在C#中将日期转换为MM/DD/YY格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/420623/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:43:52  来源:igfitidea点击:

How to Convert date into MM/DD/YY format in C#

c#date

提问by RBS

In My Asp.net webpage I need to display today's date into one of the textbox , so in my form load I wrote the following code

在我的 Asp.net 网页中,我需要将今天的日期显示到其中一个文本框中,因此在我的表单加载中我编写了以下代码

textbox1.text = System.DateTime.Today.ToShortDateString();

this line is giving me date like 1/7/09 but I want date like 01/07/09 , Is there anyway I can conver this date into mm/dd/yy format in C#?

这条线给了我像 1/7/09 这样的日期,但我想要像 01/07/09 这样的日期,无论如何我可以在 C# 中将此日期转换为 mm/dd/yy 格式?

采纳答案by Jon Skeet

 DateTime.Today.ToString("MM/dd/yy")

Look at the docs for custom date and time format stringsfor more info.

查看自定义日期和时间格式字符串的文档以获取更多信息。

(Oh, and I hope this app isn't destined for other cultures. That format could really confuse a lot of people... I've never understood the whole month/day/year thing, to be honest. It just seems weird to go "middle/low/high" in terms of scale like that.)

(哦,我希望这个应用程序不是为其他文化设计的。这种格式真的会让很多人感到困惑......老实说,我从来没有理解整个月/日/年的事情。这看起来很奇怪就这样的规模而言,达到“中/低/高”。)

回答by Kon

Look into using the ToString()method with a specified format.

研究使用ToString()具有指定格式的方法。

回答by BenAlabaster

Have you tried the following?:

您是否尝试过以下操作?:

textbox1.text = System.DateTime.Today.ToString("MM/dd/yy");

Be aware that 2 digit years could be bad in the future...

请注意,未来 2 位数年份可能会很糟糕......

回答by bruno

DateTime.Today.ToString("MM/dd/yy")

Look at the docs for custom date and time format strings for more info.

(Oh, and I hope this app isn't destined for other cultures. That format could really confuse a lot of people... I've never understood the whole month/day/year thing, to be honest. It just seems weird to go "middle/low/high" in terms of scale like that.)

DateTime.Today.ToString("MM/dd/yy")

查看自定义日期和时间格式字符串的文档以获取更多信息。

(哦,我希望这个应用程序不是为其他文化设计的。这种格式真的会让很多人感到困惑......老实说,我从来没有理解整个月/日/年的事情。这看起来很奇怪就这样的规模而言,达到“中/低/高”。)

Others cultures really are a problem. For example, that code in portugues returns someting like 01-01-01 instead of 01/01/01. I also don't undestand why...

其他文化确实是一个问题。例如,葡萄牙语中的代码返回类似于 01-01-01 而不是 01/01/01 的内容。我也不明白为什么...

To resolve that problem i do someting like this:

为了解决这个问题,我做这样的事情:

        IFormatProvider yyyymmddFormat = new System.Globalization.CultureInfo(String.Empty, false);
        return date.ToString("MM/dd/yy", yyyymmddFormat);

回答by Chirag Thakar

See, here you can get only date by passing a format string. You can get a different date format as per your requirement as given below for current date:

看,在这里您只能通过传递格式字符串来获取日期。您可以根据您的要求获得不同的日期格式,如下所示:

DateTime.Now.ToString("M/d/yyyy");

Result :"9/1/2016"

结果:“9/1/2016”

DateTime.Now.ToString("M-d-yyyy");

Result :"9-1-2016"

结果:“9-1-2016”

DateTime.Now.ToString("yyyy-MM-dd");

Result :"2016-09-01"

结果:“2016-09-01”

DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");

Result :"2016-09-01 09:20:10"

结果:“2016-09-01 09:20:10”

For more details take a look at MSDN reference for Custom Date and Time Format Strings

有关更多详细信息,请查看自定义日期和时间格式字符串的 MSDN 参考