简体   繁体   中英

DateTime.TryParse not working in Internet Explorer

public JsonResult TimeValidation(string pickUp, string delivery)   
{

    var errorMessage = string.Empty;
    var dateTime = DateTime.MinValue;

    if (!DateTime.TryParse(pickUp, out dateTime))
        errorMessage = "Invalid date";

     if (!DateTime.TryParse(delivery, out dateTime))
        errorMessage = "Invalid date";
}
‎4‎/‎29‎/‎2015‎ ‎3‎:‎30‎:‎00‎ ‎PM pickup from ie
4‎/‎30‎/‎2015‎ ‎12‎:‎00‎:‎00‎ ‎AM delivery from ie
4/29/2015, 3:30:00 PM pickup from  firefox
4/30/2015, 12:00:00 AM delivery from firefox  

its working good in chrome and firefox but its not converting to datetime in internet explorer 11 please obseve , between date and time

Assuming this is C# (looks like it is) and it is running on the server (not actually in the browser): You should check to see what the value of System.Globalization.CultureInfo.CurrentCulture is. See if it is different for a request coming from IE vs one of your other browsers. DateTime.TryParse(string, out DateTime) uses this value to help parse the string.

For instance, the date you provided: "28/04/2015 07:59:00" will cause TryParse to return false if the current culture is en-US , but if the current culture is es-MX , then it will return true .

I'm not sure why it would be different between browsers off the top of my head, but it's at least a place to start looking.

I came across a similar issue and the issue was the javaScript method toLocaleDateString() in IE11 return string with some RTL characters! which results in invalid data and these characters aren't visible.

A simple fix using regex

toLocaleDateString().replace(/[^A-Za-z 0-9 \\.,\\?""!@#\\$%\\^&\\*\\(\\)-_=\\+;:<>\\/\\\\\\|\\}\\{\\[\\]`~]*/g, '')

I tried the same regex at the back C# but it didn't work, however, I didn't want to spend more time on this so I just applied the front end solution.

More details, source

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