日期时间是给错误,而转换错误、日期、时间

2023-09-05 23:52:15 作者:六眼飞鱼

我有串在这甲:19/8/1988

I have string in this formate : 19/8/1988

请注意:字符串DATEOFBIRTH =19/8/1988

当我使用 Datetime.parse(出生日期)它给了我无效的日期格式错误

我也无法做到这一点在 CDATE(出生日期)

I am also unable to do it in cdate(DateOfBirth)

当我输入字符串格式mm / dd / YYYY即 1988年8月19日那么它不会给我的错误。

When i enters string in format mm/dd/yyyy i.e. 8/19/1988 then it does not gives me error.

请帮我把字符串转换成日期毫米/ DDD / YYYY 格式。

Please help me to convert string into date in mm/ddd/yyyy format.

推荐答案

小写毫米表示分钟,而不是一个月,你需要使用大写中号(单个字符)。

Lowercase mm means minute instead of month, you need to use uppercase M(single character).

但你还需要使用 ParseExact CultureInfo.InvariantCulture 。否则,你目前的文化是用来获取日期分隔符它不一定 / (在许多国家是

But you also need to use ParseExact with CultureInfo.InvariantCulture. Otherwise your current culture is used to get the date separator which is not necessarily /(in many countries it is .).

因此​​,这适用于任何文化:

So this works with any culture:

DateTime.ParseExact("19/8/1988", "dd/M/yyyy", CultureInfo.InvariantCulture)

演示

的/自定义格式说明

如果您想验证一个给定的日期字符串,你可以使用 < $ C C> DateTime.TryParseExact $ :

If you want to validate a given date-string you can use DateTime.TryParseExact:

DateTime dt; 
if(DateTime.TryParseExact("19/8/1988", "dd/M/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt)) 
{
    // success, dt contains the correct date now
} 
else 
{
    // not a valid date 
}
 
精彩推荐
图片推荐