无法从'M / yy / d'确定年,月和日的顺序

时间:2022-09-27 20:23:22

When I run:
Date.Parse(Now.ToString("M/yy/d"), New Globalization.DateTimeFormatInfo() With {.ShortDatePattern = "M/yy/d"})

当我运行:Date.Parse(Now.ToString(“M / yy / d”),New Globalization.DateTimeFormatInfo()with {.ShortDatePattern =“M / yy / d”})

I am getting the error:

我收到错误:

Could not determine the order of year, month, and date from 'M/yy/d'.

Is this (strange) format somewhere prohibited? Is it a bug?

这个(奇怪的)格式是禁止的吗?这是一个错误吗?

Note
I do not know why this format is required.

注意我不知道为什么需要这种格式。

Edit
Proposed Date.ParseExact(Now.ToString("M/yy/d"), "M/yy/d",New Globalization.DateTimeFormatInfo() ) gives:

编辑Proposed Date.ParseExact(Now.ToString(“M / yy / d”),“M / yy / d”,New Globalization.DateTimeFormatInfo())给出:

String was not recognized as a valid DateTime.

Edit2
I am using NET Framework 4.0 (windows).

Edit2我正在使用.NET Framework 4.0(Windows)。

1 个解决方案

#1


1  

The problem is a result of two components:

问题是两个组成部分的结果:

  1. Your current culture uses a different date separator than the invariant culture default (/).
  2. 您当前的文化使用与不变文化默认值(/)不同的日期分隔符。

  3. When / is used as part of a format string in DateTime.Parse or DateTime.ParseExact it is not translated as a literal forward slash but rather as the date separator character from the specified format provider, whatever that is (if you want it to be taken literally, you must use the "escape sequence" %/).
  4. 当/作为DateTime.Parse或DateTime.ParseExact中格式字符串的一部分使用时,它不会被翻译为文字正斜杠,而是作为指定格式提供者的日期分隔符,无论是什么(如果你想要的话)从字面上看,你必须使用“转义序列”%/)。

So there are many ways to do this correctly:

所以有很多方法可以正确地做到这一点:

Date.ParseExact(Now.ToString("M/yy/d"), "M%/yy%/d", Nothing) 

This simply tells ParseExact to interpret the slashes literally.

这简单地告诉ParseExact按字面解释斜杠。

Date.ParseExact(Now.ToString("M/yy/d"), "M/yy/d",
              New Globalization.DateTimeFormatInfo() With {.DateSeparator = "/"})

This tells ParseExact to use a literal slash as the date separator, changing its default behavior for your current culture.

这告诉ParseExact使用文字斜杠作为日期分隔符,更改其当前文化的默认行为。

As an aside: I 'm using ParseExact instead of Parse because it is the better fit when you know that the input has a specific format; Parse attempts to determine the format on its own using heuristics, which is not appropriate in this case.

暂且不说:我使用的是ParseExact而不是Parse,因为当你知道输入有特定的格式时,它更适合;解析尝试使用启发式方法自行确定格式,这在这种情况下是不合适的。

#1


1  

The problem is a result of two components:

问题是两个组成部分的结果:

  1. Your current culture uses a different date separator than the invariant culture default (/).
  2. 您当前的文化使用与不变文化默认值(/)不同的日期分隔符。

  3. When / is used as part of a format string in DateTime.Parse or DateTime.ParseExact it is not translated as a literal forward slash but rather as the date separator character from the specified format provider, whatever that is (if you want it to be taken literally, you must use the "escape sequence" %/).
  4. 当/作为DateTime.Parse或DateTime.ParseExact中格式字符串的一部分使用时,它不会被翻译为文字正斜杠,而是作为指定格式提供者的日期分隔符,无论是什么(如果你想要的话)从字面上看,你必须使用“转义序列”%/)。

So there are many ways to do this correctly:

所以有很多方法可以正确地做到这一点:

Date.ParseExact(Now.ToString("M/yy/d"), "M%/yy%/d", Nothing) 

This simply tells ParseExact to interpret the slashes literally.

这简单地告诉ParseExact按字面解释斜杠。

Date.ParseExact(Now.ToString("M/yy/d"), "M/yy/d",
              New Globalization.DateTimeFormatInfo() With {.DateSeparator = "/"})

This tells ParseExact to use a literal slash as the date separator, changing its default behavior for your current culture.

这告诉ParseExact使用文字斜杠作为日期分隔符,更改其当前文化的默认行为。

As an aside: I 'm using ParseExact instead of Parse because it is the better fit when you know that the input has a specific format; Parse attempts to determine the format on its own using heuristics, which is not appropriate in this case.

暂且不说:我使用的是ParseExact而不是Parse,因为当你知道输入有特定的格式时,它更适合;解析尝试使用启发式方法自行确定格式,这在这种情况下是不合适的。