Microsoft Edge - C#错误 - 字符串未被识别为有效的DateTime

问题描述 投票:0回答:1

您好我的MVC控制器Get方法中的日期时间格式问题。当从Firefox,Chrome,Internet Explorer发送请求时,它工作正常但是它在Microsoft Edge浏览器请求时抛出异常:(

例外:字符串未被识别为有效的DateTime。

示例代码在这里 -

public JsonResult GetFYDetailsForDate(string date)
{
    //input date = "6/13/2018"
    DateTimeStyles dateTimeStyles = DateTimeStyles.AssumeLocal;
    CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");

    var culturedDate1 = DateTime.ParseExact(date, "M/d/yyyy", new System.Globalization.CultureInfo("en-US"));

    var culturedDate = DateTime.Parse(date, culture, dateTimeStyles);

}

如果请求来自Chrome,Firefox和Internet Explorer,则输入日期为“6/13/2018”,解析日期输出为“6/13/2018 12:00:00 AM”。

**要解决此问题,我们可以从日期字符串中提取日期,月份和年份,但我不想这样做。只想知道来自Microsoft Edge浏览器的请求有什么问题。

代码截图 - enter image description here

c# asp.net asp.net-mvc datetime microsoft-edge
1个回答
1
投票

输入日期字符串来自Microsoft Edge浏览器时出现问题。日期字符串包含字符(字符)8206,这就是C#无法解析日期并抛出错误的原因。字符串的可见长度为9,但字符串的实际长度为14.因此我从字符串中删除了这些隐藏字符,现在它的工作完全正常。

我已经习惯了清理字符串的代码片段,请建议任何更好,更有效的方法来执行此操作。

谢谢。

string sanitizedDateString = new String(inputDate.ToCharArray().Where(x => x != (Char)8206).ToArray());
© www.soinside.com 2019 - 2024. All rights reserved.