如何将字符串解析为日期 [英] How to Parse a string into a date

查看:187
本文介绍了如何将字符串解析为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下代码块:

DateTime fromdate = null;
字符串dateTime ="09-07-2010 00:00:00"
fromdate = DateTime.ParseExact(dateTime,"dd-MMM-yyyy HH:mm:ss",null);

解析时出现错误.谁能告诉我为什么会出错?

在此先感谢您.

I have a code block as below:

DateTime fromdate = null;
String dateTime = "09-07-2010 00:00:00"
fromdate = DateTime.ParseExact(dateTime, "dd-MMM-yyyy HH:mm:ss", null);

It is giving error while Parsing. Can anyone tell me why it is giving error?

Thanks in advance.

推荐答案

您粘贴了确切的代码片段吗?为什么会有"dd-MM M -yyyy ...".我不认为三元组M是正确的月份格式.
Did you pasted exact code fragment? Why there is "dd-MMM-yyyy ...". I don''t think tripple M is correct month format.


bedathur_ganesh,

您是否尝试过使用 .Parse 而不是 .ParseExact ?如果您的日期的格式类似于字符串dateTime的值,则由于 dateTime 的值的格式为dd-MM-yyyy hh:mm:ss,并且您想精确地将其解析为字符串,因此使用ParseExact将会失败. dd-MMM-yyyy HH:mm:ss.

您可以从此链接中了解更多信息. [
Hi bedathur_ganesh,

Have you tried using .Parse instead of .ParseExact? If your date is in the format like the value of string dateTime, then using ParseExact will fail since the format of the dateTime''s value is dd-MM-yyyy hh:mm:ss and you want to parse it exactly in dd-MMM-yyyy HH:mm:ss.

You can read more from this link.[^]

@helianthus87 - MMM is allowed format for month. This is the 3-letter representation of month (e.g. Jan, Feb, Mar, etc.).

Hope this helps.


这篇关于如何将字符串解析为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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