简体   繁体   English

字符串未被识别为有效的日期时间。 即使使用 parse Exact

[英]string was not recognized as a valid DateTime. even if using parse Exact

im using parse exactl to convert the following string but still give me an error:我正在使用 parse exactl 来转换以下字符串,但仍然给我一个错误:

Tue Oct 06 2020 15:22:59 GMT 0200 (Central European Summer Time)

here is my convert:这是我的转换:

  DateTime frm = DateTime.ParseExact(dtFrom, "yyyy/MM/dd", CultureInfo.InvariantCulture);

Use Regex:使用正则表达式:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "Tue Oct 06 2020 15:22:59 GMT 0200 (Central European Summer Time)";
            string pattern = @"^(?'date'.*)(?'sign'.)(?'offset'\d{4})\s(?'ending'\([^)]+\))$";
            Match match = Regex.Match(input, pattern);
            string dateStr = match.Groups["date"].Value;
            string sign = match.Groups["sign"].Value;
            string offset = match.Groups["offset"].Value;
            switch (sign)
            {
                case " ":
                    dateStr = dateStr + " +" + offset;
                    break;
                case "+":
                    dateStr = dateStr + sign + offset;
                    break;
                case "-":
                    dateStr = dateStr + sign + offset;
                    break;
            }
            DateTime date = DateTime.ParseExact(dateStr,"ddd MMM dd yyyy HH:mm:ss \\G\\M\\T zzz", System.Globalization.CultureInfo.InvariantCulture);
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对于某些确切的日期,发生了“字符串未被识别为有效的DateTime。”错误 - "String was not recognized as a valid DateTime.” error occurs for some exact dates 无法将字符串识别为有效的DateTime。 在Windows Server 2012上使用ParseExact - String was not recognized as a valid DateTime. using ParseExact on Windows Server 2012 解析正确时,无法将字符串识别为有效的DateTime - String was not recognized as a valid DateTime when parse exact 字符串日期未被识别为有效的日期时间。 - String date was not recognized as a valid DateTime.' 解析时,字符串未被识别为有效的DateTime - String was not recognized as a valid DateTime when parse exact 错误消息为“字符串未被识别为有效的DateTime。” - Error Message as “String was not recognized as a valid DateTime.” 无法将字符串识别为有效的DateTime。 引发异常 - String was not recognized as a valid DateTime. Throws an Exception 错误:字符串未被识别为有效的DateTime。 - Error: String was not recognized as a valid DateTime. 获取错误“字符串未被识别为有效的DateTime”。 - Getting error “String was not recognized as a valid DateTime.” 字符串未被识别为有效的日期时间。 将字符串转换为日期时间 - String was not recognized as a valid DateTime. Converting string to DateTime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM