DateTime.ParseExact字符串格式异常 [英] DateTime.ParseExact string format exception

查看:117
本文介绍了DateTime.ParseExact字符串格式异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下C#代码将字符串转换为datetime,

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

每次我将dateTo作为1/1/2010,它失败,而是需要字符串为01 / 01/2010。



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

解决方案

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

 M / d / yyyy

请注意,首都 M 是重要的 - 小写 m 是分钟的占位符。



您将在日期格式字符串这里找到更多相关信息。 p>

您可以使用以下Powershell命令来测试它们。

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


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 I pass dateTo as 1/1/2010 it fails, instead it needs the string to be 01/01/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"

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.

You can use the following Powershell command to test them.

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

这篇关于DateTime.ParseExact字符串格式异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆