且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在.NET自定义日期格式的日期时间解析

更新时间:2023-02-16 17:36:05

将它总是与订单

string pattern = "'ORDER'yyyyMMddHHmmss";
DateTime dt;
if (DateTime.TryParseExact(text, pattern, CultureInfo.InvariantCulture, 
                           DateTimeStyles.None,
                           out dt))
{
    // dt is the parsed value
} 
else 
{
    // Invalid string
}

如果字符串是无效的应该抛出一个异常,然后使用 DateTime.ParseExact ,而不是 DateTime.TryParseExact

If the string being invalid should throw an exception, then use DateTime.ParseExact instead of DateTime.TryParseExact

如果它并不总是以订单,然后做任何你需要在开始为了得到公正的日期和时间部分,并从上面的格式模式'订单'。

If it doesn't always begin with "ORDER" then do whatever you need to in order to get just the date and time part, and remove "'ORDER'" from the format pattern above.