C# DateTime.Today.ToString("dd/mm/yyyy") 返回无效的 DateTime 值

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

DateTime.Today.ToString("dd/mm/yyyy") returns invalid DateTime Value

c#datetime

提问by Farhad-Taran

I am trying to get todays date in GBR format but,

我正在尝试以 GBR 格式获取今天的日期,但是,

DateTime.Now.ToString("dd/mm/yyyy");

returns with the value of 08/00/2013

返回值 08/00/2013

So 00in the month group was returned instead of returning the month 04.

所以00在月份组被返回而不是返回月份04

Any ideas why this happened?

任何想法为什么会发生这种情况?

采纳答案by Tim Schmelter

Lower mmmeans minutes, so

mm意味着分钟,所以

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

or

或者

DateTime.Now.ToString("d");  

or

或者

DateTime.Now.ToShortDateString()

works.

作品

Standard Date and Time Format Strings

标准日期和时间格式字符串

回答by Oded

It should be MMfor months. You are asking for minutes.

应该是MM几个月。你要求几分钟。

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

See Custom Date and Time Format Stringson MSDN for details.

有关详细信息,请参阅MSDN 上的自定义日期和时间格式字符串

回答by Arshad

use MM(months) instead of mm(minutes) :

使用MM(months) 而不是mm(minutes) :

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

check herefor more format options.

检查这里更多的格式选项。

回答by Soner G?nül

Use MMfor months. mmis for minutes.

使用MM数月。mm是几分钟。

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

You probably run this code at the begining an hour like (00:00, 05.00, 18.00) and mmgives minutes (00) to your datetime.

您可能像 ( 00:00, 05.00, 18.00)一样在一小时开始时运行此代码,并mm00您的日期时间提供分钟 ( )。

From Custom Date and Time Format Strings

Custom Date and Time Format Strings

"mm" --> The minute, from 00 through 59.

"MM" --> The month, from 01 through 12.

"mm" --> 分钟,从 00 到 59。

"MM" --> 月份,从 01 到 12。

Here is a DEMO. (Which the monthpart of first line depends on which time do you run this code ;))

这是一个DEMO. (第一行的月份部分取决于您运行此代码的时间;)

回答by Klitos Kyriacou

In addition to what the other answers have said, note that the '/' character in "dd/MM/yyyy" is not a literal character: it represents the date separator of the current user's culture. Therefore, if the current culture uses yyyy-MM-dd dates, then when you call toString it will give you a date such as "31-12-2016" (using dashes instead of slashes). To force it to use slashes, you need to escape that character:

除了其他答案所说的内容之外,请注意“dd/MM/yyyy”中的“/”字符不是文字字符:它代表当前用户文化的日期分隔符。因此,如果当前文化使用 yyyy-MM-dd 日期,那么当您调用 toString 时,它将为您提供一个日期,例如“31-12-2016”(使用破折号而不是斜线)。要强制它使用斜杠,您需要转义该字符:

DateTime.Now.ToString("dd/MM/yyyy")     --> "19-12-2016" for a Japanese user
DateTime.Now.ToString("dd/MM/yyyy")     --> "19/12/2016" for a UK user
DateTime.Now.ToString("dd\/MM\/yyyy") --> "19/12/2016" independent of region

回答by twinkle

There are only minor error.Use MM instead of mm ,so it will be effective write as below:

只有轻微的错误。使用 MM 而不是 mm ,所以它会有效 写如下:

 @DateTime.Now.ToString("dd/MM/yyy")