简体   繁体   中英

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:

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

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM