admin管理员组文章数量:1327258
I'm passing a date back to my MVC controller via AJAX using:
date.GetDate().toLocaleDateString();
This will produce a value of "4/5/2014"... When I try and convert this inside my controller using:
DateTime myDate = DateTime.ParseExact(request.Params["myDate"], "dd/MM/yyyy", CultureInfo.InvariantCulture);
I get "String was not recognized as a valid 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.
Is there anyway to fix the format ing from javascript without have to rip the string apart into day, month, year and reassembling it in the proper format?
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...
I'm passing a date back to my MVC controller via AJAX using:
date.GetDate().toLocaleDateString();
This will produce a value of "4/5/2014"... When I try and convert this inside my controller using:
DateTime myDate = DateTime.ParseExact(request.Params["myDate"], "dd/MM/yyyy", CultureInfo.InvariantCulture);
I get "String was not recognized as a valid 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.
Is there anyway to fix the format ing from javascript without have to rip the string apart into day, month, year and reassembling it in the proper format?
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...
Share Improve this question edited Jun 4, 2014 at 20:40 Hidan asked Jun 4, 2014 at 17:41 HidanHidan 3571 gold badge5 silver badges18 bronze badges 6-
Add a watch for
myViewBagDate == myRequestDate
. – Michael Liu Commented Jun 4, 2014 at 19:20 - Strange, I get "This expression causes side effects and will not be evaluated" when trying to do a watch on that expression. – Hidan Commented Jun 4, 2014 at 19:39
-
1
What about
myViewBagDate.Length
andmyRequestDate.Length
? – Michael Liu Commented Jun 4, 2014 at 20:02 - So I tried in the immediate window and here are the results: ? myRequestDate == myViewBagDate false ? myRequestDate "3/5/2014" ? myViewBagDate "3/2/2014" – Hidan Commented Jun 4, 2014 at 20:03
- Wow, myRequestDate.Length = 13 and myViewBagDate.Length = 8 ?!?! When viewed the are both the same though! – Hidan Commented Jun 4, 2014 at 20:05
4 Answers
Reset to default 2Character 8206 (U+200E) is the Unicode LEFT-TO-RIGHT MARK (which is invisible). Try to figure out where it's ing 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);
This code:
var d=new Date();
console.log(d.toString());
In windows 8.1 Chrome does this output
"Wed Jun 04 2014 20:38:23 GMT+0200 (Hora de verano romance)"
But in Cromium in Kubuntu
"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;
}
Use the following code at the client side:
(date.GetDate()).toDateString();
At your controller side:
var date = Convert.ToDateTime(Request.Params["myDate"]);
Just change the format string you use to match what you get from the JavaScript date.
DateTime.ParseExact(request.Params["myDate"], "d/M/yyyy", CultureInfo.InvariantCulture);
本文标签: cJavascript date to ASPNET date String was not recognized as a valid DateTimeStack Overflow
版权声明:本文标题:c# - Javascript date to ASP.NET date: String was not recognized as a valid DateTime - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742211567a2433808.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论