Hidan
Hidan

Reputation: 357

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:

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 coming 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...

enter image description here

Upvotes: 3

Views: 3908

Answers (4)

Yared
Yared

Reputation: 2472

Use the following code at the client side:

    (date.GetDate()).toDateString();

At your controller side:

    var date = Convert.ToDateTime(Request.Params["myDate"]);

Upvotes: 2

Michael Liu
Michael Liu

Reputation: 55509

Character 8206 (U+200E) is the Unicode LEFT-TO-RIGHT MARK (which is invisible). 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);

Upvotes: 2

FRL
FRL

Reputation: 776

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;

    }

Upvotes: 2

Peter Olson
Peter Olson

Reputation: 143037

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

Upvotes: 1

Related Questions