Steve Ward
Steve Ward

Reputation: 1217

.Net DateTime format not recognised as a parameter - called from Javascript

I have a MVC action method which takes a DateTime as a parameter. This action is called from Javascript.

I can't get it to recognise dates with times. Im using an invariant format e.g. escape("2011/09/22 12:00:00") but this value isn't being bound in the action.

If I supply just the date part e.g. escape("2011/09/22"), that works OK but the value with the time should also be in a correct format but it isn't being bound by the model binder ...

Upvotes: 1

Views: 842

Answers (3)

sean.c
sean.c

Reputation: 21

I had problems getting dates into a asp.net webservice; took me a while to find out the format which was required. But once i found it i had enough information to create a small stringify-type js function:

dateToString = function (d) {
        function _zeroPad(v) {
            return v < 10 ? '0' + v : v;
        }
                return d.getUTCFullYear()           + '-' +
              _zeroPad(d.getUTCMonth() + 1) + '-' +
              _zeroPad(d.getUTCDate())      + 'T' +
              _zeroPad(d.getUTCHours())     + ':' +
              _zeroPad(d.getUTCMinutes())   + ':' +
              _zeroPad(d.getUTCSeconds())   + 'Z';
};

Upvotes: 1

Steve Ward
Steve Ward

Reputation: 1217

I found a solution which works for me (and probably produces a date in a format similar to Sean's answer) ...

I just create a date in Javascript and posted this as data as part of an Ajax request

E.g.

var d = new Date("2011/09/22 12:00:00");

resulted in a post of a date in the format of

2011-09-22T02:00:00.000Z

which was accepted by MVC's model binding. The hour is different because of the time format (I'm in Australia)

Upvotes: 1

Aliostad
Aliostad

Reputation: 81690

try

 escape("2011-09-22 12:00:00") 

Upvotes: 0

Related Questions