[英]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.