DateTime.ParseExact字符串格式异常

编程入门 行业动态 更新时间:2024-10-27 05:27:57
本文介绍了DateTime.ParseExact字符串格式异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想一个字符串转换成日期时间与下面的C#代码,

I am trying to convert a string into datetime with the following C# code,

DateTime dTo = DateTime.ParseExact(dateTo, "mm/dd/yyyy", CultureInfo.InvariantCulture);

eachtime我通过dateTo为1/1/2010失败,而是需要字符串为01 / 01/2010。

eachtime I pass dateTo as 1/1/2010 it fails, instead it needs the string to be 01/01/2010.

我应该使用什么字符串格式,同时支持01/01/2010和1/1/2010?

What string format should I use to support both 01/01/2010 and 1/1/2010?

推荐答案

使用以下日期格式表达式将允许您使用单或双位数的日和月的元素。

Using the following date format expression will allow you to use either single or double digit day and month elements.

"M/d/yyyy"

请注意,资本 M 是显著 - 小写 M 是分钟占位符

Note that the capital M is significant - a lower case m is the placeholder for minutes.

你会发现相关的日期格式字符串这里。

You will find more information related to date format strings here.

您可以使用下面的PowerShell命令来测试它们。

You can use the following Powershell command to test them.

[DateTime]::ParseExact('01/01/2010', 'M/d/yyyy', $null)

更多推荐

DateTime.ParseExact字符串格式异常

本文发布于:2023-11-22 15:22:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617957.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   异常   格式   DateTime   ParseExact

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!