[英]Javascript date to ASP.NET date: String was not recognized as a valid DateTime
I'm passing a date back to my MVC controller via AJAX using: 我使用以下命令通过AJAX将日期传递回我的MVC控制器:
date.GetDate().toLocaleDateString();
This will produce a value of "4/5/2014"... When I try and convert this inside my controller using: 这将产生一个值“ 4/5/2014” ...当我尝试使用以下方法在控制器内部进行转换时:
DateTime myDate = DateTime.ParseExact(request.Params["myDate"], "dd/MM/yyyy", CultureInfo.InvariantCulture);
I get "String was not recognized as a valid DateTime." 我得到“字符串未被识别为有效的DateTime。” Which makes sense because the format of the string isn't correct... When I hardcode the string to: "04/05/2014" it will fix my issue.
这是有道理的,因为字符串的格式不正确...当我将字符串硬编码为“ 04/05/2014”时,它将解决我的问题。
Is there anyway to fix the format coming from javascript without have to rip the string apart into day, month, year and reassembling it in the proper format? 无论如何,是否有必要修复来自javascript的格式,而不必将字符串分成天,月,年并以正确的格式重新组装?
Any advice would be much appreciated! 任何建议将不胜感激!
Thank you 谢谢
Additional info: 附加信息:
string myRequestDate = request.Params["myDate"];
string myViewBagDate = ViewBag.MyDate;
//This line passes
DateTime date1 = DateTime.ParseExact(myViewBagDate, "d/M/yyyy", CultureInfo.InvariantCulture);
//This line fails...
DateTime date5 = DateTime.ParseExact(myRequestDate, "d/M/yyyy", CultureInfo.InvariantCulture);
When I add a watch on both of the string variables the values are identical in every way I can see but for some reason the second line fails... 当我在两个字符串变量上添加监视时,在我能看到的所有方式上值都相同,但是由于某种原因第二行失败了...
So when I look at the myRequestDate as a char array I see there is a bunch of stuff in there that doesn't look like a date at all... 因此,当我将myRequestDate视为一个char数组时,我发现其中有一堆东西看起来根本不像日期...
只需更改您使用的格式字符串以匹配您从JavaScript日期获得的格式字符串即可。
DateTime.ParseExact(request.Params["myDate"], "d/M/yyyy", CultureInfo.InvariantCulture);
This code: 这段代码:
var d=new Date();
console.log(d.toString());
In windows 8.1 Chrome does this output 在Windows 8.1中,Chrome执行此输出
"Wed Jun 04 2014 20:38:23 GMT+0200 (Hora de verano romance)"
But in Cromium in Kubuntu 但是在库本图的Cromium
"Wed Jun 04 2014 20:38:23 GMT+0200 (CEST)"
Very similar, but other browser return diferent kind of formats, i use this function, this work fine with various kinds of formats. 非常相似,但是其他浏览器返回不同类型的格式,我使用此功能,可以在各种格式下正常工作。
public static DateTime ParseDate(string value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
string[] formats = {"ddd MMM dd yyyy hh:mm:ss 'UTC'zzz",
"ddd MMM d yyyy hh:mm:ss 'UTC'zzz",
"ddd MMM d hh:mm:ss 'UTC'zzz yyyy",
"ddd MMM dd hh:mm:ss 'UTC'zzz yyyy",
"ddd MMM dd yyyy hh:mm:ss 'GMT'zzz",
"ddd MMM d yyyy hh:mm:ss 'GMT'zzz",
"ddd MMM d hh:mm:ss 'GMT'zzz yyyy",
"ddd MMM dd hh:mm:ss 'GMT'zzz yyyy",
"dd-MM-yyyy",
"yyyy-MM-dd'T'hh:mm:ss"
};
DateTime fecha;
//Try the default
if (!DateTime.TryParse(value, out fecha))
{
if (!DateTime.TryParseExact(value, formats,
new CultureInfo("en-US"),
DateTimeStyles.None, out fecha))
{
if (value.Length > 24)
{
return ParseDate(value.Substring(0, 24));
}
else
{
throw new FormatException();
}
}
}
return fecha;
}
Character 8206 (U+200E) is the Unicode LEFT-TO-RIGHT MARK (which is invisible). 字符8206(U + 200E)是Unicode左到右标记(不可见)。 Try to figure out where it's coming from and remove it at the source.
尝试找出它的来源,并从源头将其删除。
As a workaround, you can strip out those characters before you parse the date: 解决方法是,您可以在解析日期之前删除这些字符:
myRequestDate = myRequestDate.Replace("\u200E", "");
DateTime date5 = DateTime.ParseExact(myRequestDate, "d/M/yyyy", CultureInfo.InvariantCulture);
Use the following code at the client side: 在客户端使用以下代码:
(date.GetDate()).toDateString();
At your controller side: 在您的控制器端:
var date = Convert.ToDateTime(Request.Params["myDate"]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.