DateTime.Parse可以格式化异常格式的字符串吗? [英] DateTime.Parse can format unusual format strings?

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

问题描述

我正在查看应用程序中的代码(由其他人编写),在某些情况下它可以正常工作,在某些情况下它提供了异常,它实际上是在日期时间转换字符串,这是代码

I was looking at a code in an application (Someone else wrote it),on some cases it worked fine and on some cases it gave exceptions,it was actually converting strings in datetime,here is the code

//5000 is the year,but what about "1" is it month or day ?,if its month
//then what about the day ?
DateTime time =  DateTime.Parse("1.5000");//1.5000 doesn't looks a date to me ?
time.ToString();//returns "1/1/5000 12:00:00 AM"

//where as if I give this string to DateTime.Parse();
time =  DateTime.Parse("2341.70");
//FormatException was unhandled
//String was not recognized as a valid DateTime.

一个令人困惑的想法

字符串 3.5000(与1.5000模式匹配)求值,这是否表示3-3-5000或1-3-5000,格式不明确,不清楚且令人困惑!

A Confusing thought
How does this string "3.5000" (it matches the 1.5000 pattern) evaluates , does this means 3-3-5000 or 1-3-5000 ,the format is ambiguous its unclear and confusing !

我的问题是


  1. DateTime.Parse可以期望哪种格式?

  2. 上面的代码中发生了什么?

  3. 建议改进代码?


推荐答案

许多人都对解析成功的可能原因发表了评论,但您的问题似乎包含几个单独的部分...

Many people have commented on the possible reasons for the parse that you have seen being successful but your question seems to have several separate parts...

1。 DateTime.Parse可以期望哪种格式?

DateTime.Parse已被编写为包含尽可能多的内容。它几乎可以找到将其制成DateTime的所有内容,它将尽力做到这一点,这意味着除了通常熟悉的yyyy-MM-dd类型的格式外,还有更多奇怪的格式,例如M.yyyy或yyyy.M等。

DateTime.Parse has been written to be as inclusive as possible. Pretty much anything that it can find someway to make into a DateTime it will do its best to do so which means in addition to the usual familiar yyyy-MM-dd type formats more strange ones like M.yyyy or yyyy.M and so on.

2。上面的代码中发生了什么?

这很复杂,因为 DateTime.Parse 方法是本身很复杂。您可能可以在某处找到源代码,但是复杂性使我很难遵循。在无法提供确切细节的情况下,我将按照上述相同的方式回答。发生的事情是该框架正在尽最大努力为您提供日期,而不会引发异常。给出的日期是对您的意思的最佳猜测。

That is very complicated because the DateTime.Parse method is itself very complicated. You can probably fidn the source code out there somewhere but the complexity made it very hard for me to follow. Without being able to give precise details I'm going to answer this the same as above. What is happening is that the framework is trying its best to give you a date back and not throw an exception. The date it gives is the best guess as to what you meant.

3。改善代码的建议?

这听起来像是如果您正在解析异常,则您以意外的格式传递日期。不知道这些输入是什么,很难说。不过有两件事可以改善您的代码。确保使用单个一致的日期格式,然后使用 DateTime.ParseExact 确保其符合正确的格式。您将以这种方式消除所有歧义,但会牺牲灵活性。

It sounds like if you are getting parse exceptions that you are passing dates in formats that are unexpected. Without knowing what those inputs are its hard to say. Two things could improve your code though. Making sure a single consistent date format is used and then using DateTime.ParseExact to ensure that it conforms to the right format. You will remove all ambiguity this way but you will sacrifice flexibility.

第二个选择是使用 DateTime.TryParse 。这将尝试解析您的日期,然后返回一个布尔值,说明日期是否成功。如果成功,日期解析将在ref参数中返回。这不会使您的代码更好地识别未知的日期格式,但是会在代码无法解析时通知您代码,您可以对其进行处理(例如,通过提供报告错误格式并建议正确格式的用户反馈,或只需将其记录下来即可)。

The second option is to use DateTime.TryParse. This will attempt to parse your date and then return a boolean saying whether it succeeded or not. If successful the date parse will be returned in a ref parameter. This won't make your code any better at recognising unknown date formats but will let your code know when such an unparsable format crops up and you can deal with it (eg by providing user feedback reporting the wrong format and suggesting a correct one, or just by logging it or something else).

最好的方法主要取决于输入的来源。如果是用户输入,那么我将选择第二个选项。如果是自动输入,则可能要确保输入是标准化的,然后使用第一个选项。当然,情况总是变化的,所以这不是一个硬性规定。 :)

What the best method is depends mostly on where your input is coming from. If it is user input then I'd go with the second option. If it is automated input then you probably want to make sure your input is standardized and then use the first option. Of course circumstances always vary so this is not a hard and fast rule. :)

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

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