DateTime.TryParse将十进制转换为datetime [英] DateTime.TryParse converts decimal to datetime

查看:114
本文介绍了DateTime.TryParse将十进制转换为datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码行返回true(它不应该)....并将1.0228转换为datetime ...

The following line of code return true (which it should not)....and convert 1.0228 into datetime...

DateTime.TryParse(1.0228,out temporaryDateTimeValue)

有人请帮助我。

推荐答案


以下代码行返回true(它不应该)....并将1.0228转换为datetime。 ..

The following line of code return true (which it should not)....and convert 1.0228 into datetime...



DateTime.TryParse(1.0228,out temporaryDateTimeValue)

这不会编译。

但是,如果你用引号括起来(并清理一下),

However, if you wrap it in quotes (and clean it up a little bit),

bool success = DateTime.TryParse("1.0228", out temporaryDateTimeValue);

然后,是的,你会得到 true 背部。您需要阅读文档才能理解为什么,但基本上可以使用多种不同的格式来格式化日期,并且您偶然发现(可能 M.yyyy ?)

then, yes, you will get true back. You need to read the documentation to understand why, but basically, there are many different ways to format dates and you stumbled on one (maybe M.yyyy?).

如果您不想解析,可以建议

If you don't want it to parse, may I suggest

bool success = DateTime.TryParseExact(
                   "1.0228",
                   "yyyyMMdd", 
                   CultureInfo.InvariantCulture,
                   DateTimeStyles.None,
                   out temporaryDateTimeValue
               );

然后成功 false

我从文档


字符串 s 使用当前 DateTimeFormatInfo 对象中的格式化信息进行解析,该对象由当前线程文化隐式提供。

The string s is parsed using formatting information in the current DateTimeFormatInfo object, which is supplied implicitly by the current thread culture.

如果可能,此方法尝试忽略无法识别的数据,并填写当前日期缺少的月份,日期和年份信息。如果s只包含日期和时间,则此方法假设时间为午夜12:00。 s中的任何前导,内部或尾随的空格字符都被忽略。日期和时间可以包含一对前导和后面的NUMBER个SIGN字符('#',U + 0023),并且可以跟踪一个或多个NULL字符(U + 0000)。

This method tries to ignore unrecognized data, if possible, and fills in missing month, day, and year information with the current date. If s contains only a date and no time, this method assumes the time is 12:00 midnight. Any leading, inner, or trailing white space character in s is ignored. The date and time can be bracketed with a pair of leading and trailing NUMBER SIGN characters ('#', U+0023), and can be trailed with one or more NULL characters (U+0000).

因为 DateTime.TryParse(String,DateTime)方法会尝试解析字符串使用当前文化的格式化规则表示日期和时间,尝试解析不同文化中的特定字符串可能会失败或返回不同的结果。如果特定日期和时间格式将在不同的区域设置中解析,请使用 DateTime.TryParse(String,IFormatProvider,DateTimeStyles,DateTime)方法或 TryParseExact 方法,并提供格式说明符。

Because the DateTime.TryParse(String, DateTime) method tries to parse the string representation of a date and time using the formatting rules of the current culture, trying to parse a particular string across different cultures can either fail or return different results. If a specific date and time format will be parsed across different locales, use the DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime) method or one of the overloads of the TryParseExact method and provide a format specifier.

基本上, TryParse 尝试非常难以解析您给出的字符串(尽管 Try 实际上是指该方法返回一个bool for success / failure indication)。

Basically, TryParse "tries" very hard to parse the string you give it (although the "Try" really refers to the fact that the method returns a bool for success/failure indication).

这篇关于DateTime.TryParse将十进制转换为datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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