相关疑难解决方法(0)

DateTime.Parse可以格式化异常格式的字符串吗?

我正在查看应用程序中的代码(其他人写的),在某些情况下它运行正常,在某些情况下它给出了异常,它实际上是在datetime中转换字符串,这里是代码

//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.
Run Code Online (Sandbox Code Playgroud)

一个令人困惑的想法
这个字符串"3.5000"(它与1.5000模式匹配)如何评估,这意味着3-3-5000或1-3-5000,格式模糊不清,令人困惑!

我的问题是,

  1. DateTime.Parse期望什么样的格式?
  2. 上面的代码中发生了什么?
  3. 建议改进代码?

.net c# datetime

5
推荐指数
1
解决办法
648
查看次数

标签 统计

.net ×1

c# ×1

datetime ×1