Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 字符串未被识别为有效的日期时间。即使使用parse-Exact_C# - Fatal编程技术网

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

C# 字符串未被识别为有效的日期时间。即使使用parse-Exact,c#,C#,我正在使用parse exactl转换以下字符串,但仍然给我一个错误: Tue Oct 06 2020 15:22:59 GMT 0200 (Central European Summer Time) 这是我的信: DateTime frm = DateTime.ParseExact(dtFrom, "yyyy/MM/dd", CultureInfo.InvariantCulture); 使用正则表达式: using System; using System.Coll

我正在使用parse exactl转换以下字符串,但仍然给我一个错误:

Tue Oct 06 2020 15:22:59 GMT 0200 (Central European Summer Time)
这是我的信:

  DateTime frm = DateTime.ParseExact(dtFrom, "yyyy/MM/dd", CultureInfo.InvariantCulture);
使用正则表达式:

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);
        }

    }
}

parse-exact的第二个参数是一个字符串(或字符串数组),它定义了您提供给它的日期字符串的格式,而不是您从中删除的日期字符串的格式。您似乎收到了某种RFC822格式,请尝试以下帖子:““精确解析”意味着您需要使用精确的格式。您的输入字符串与格式
yyyy/MM/dd
不匹配。这个类似问题的另一个想法是:这个问题并非无关紧要。ParseExact需要时区偏移的加号或减号。