简体   繁体   English

Microsoft Edge-C#错误-无法将字符串识别为有效的DateTime

[英]Microsoft Edge - C# Error - String was not recognized as a valid DateTime

Hi its facing issue with datetime formatting in my MVC Controller Get method. 嗨,我的MVC Controller Get方法中日期时间格式面临的问题。 Its working fine when request send from Firefox, Chrome, Internet Explorer, But it throws an exception while request comes from Microsoft Edge browser :( 从Firefox,Chrome,Internet Explorer发送请求时,它的工作正常,但是当请求来自Microsoft Edge浏览器时,它会引发异常:

Exception : String was not recognized as a valid DateTime. 异常:字符串未被识别为有效的DateTime。

Sample Code is here - 示例代码在这里-

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

}

Imput Date is "6/13/2018" and Parsed Date output is "6/13/2018 12:00:00 AM" if request comes from Chrome, Firefox and Internet Explorer. 如果请求来自Chrome,Firefox和Internet Explorer,则输入日期为“ 6/13/2018”,解析日期输出为“ 6/13/2018 12:00:00 AM”。

** To resolve this we can extract date, month and year from date string, but I don't want to do that. **要解决此问题,我们可以从日期字符串中提取日期,月份和年份,但是我不想这样做。 Just want to know what is wrong with request comes from Microsoft Edge browser. 只想知道来自Microsoft Edge浏览器的请求出了什么问题。

Code screenshot - 代码屏幕截图- 在此处输入图片说明

There is a problem with input date string when it comes from Microsoft Edge browser. 来自Microsoft Edge浏览器的输入日期字符串有问题。 Date string contains characters (Char)8206 that's why C# unable to parse the date and throws an error. 日期字符串包含字符(Char)8206 ,这就是C#无法解析日期并引发错误的原因。 Visible length of string is 9, but actual length of string is 14. So I have removed those hidden characters from string and its working perfectly fine now. 可见的字符串长度为9,但实际的字符串长度为14。因此,我从字符串中删除了那些隐藏的字符,并且现在可以正常工作了。

Below code snippet I have used to sanitize string, please suggest any better and efficient way to do this. 在我以前用来清理字符串的代码段下面,请提出实现此目的的更好,更有效的方法。

Thanks you. 谢谢。

string sanitizedDateString = new String(inputDate.ToCharArray().Where(x => x != (Char)8206).ToArray());

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

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