1

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

3 Answers 3

1

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)

Sign up to request clarification or add additional context in comments.

Comments

1

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';
};

1 Comment

Thanks Sean for your first answer. My answer uses a date in the same format as this ... thanks for the help.
0

try

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.