2

I've read a lot of issues about setting the correct datetime but can't get it right.

My MVC3 approach is database first. The model is auto generated and manually changed;

    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yy}")]
    public Nullable<System.DateTime> Datum { get; set; }

in my view it's a straight forward razor;

    <div class="editor-field">
        @Html.EditorFor(model => model.Datum)
        @Html.ValidationMessageFor(model => model.Datum)
    </div>

in Shared_Layout I added this

jQuery(function ($) {

$.datepicker.regional['nl'] = {
            closeText: 'Sluiten',
            prevText: '←',
            nextText: '→',
            currentText: 'Vandaag',
            monthNames: ['januari', 'februari', 'maart', 'april', 'mei', 'juni',
                'juli', 'augustus', 'september', 'oktober', 'november', 'december'],
            monthNamesShort: ['jan', 'feb', 'maa', 'apr', 'mei', 'jun',
                'jul', 'aug', 'sep', 'okt', 'nov', 'dec'],
            dayNames: ['zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'],
            dayNamesShort: ['zon', 'maa', 'din', 'woe', 'don', 'vri', 'zat'],
            dayNamesMin: ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za'],
            weekHeader: 'Wk',
            dateFormat: 'dd-mm-yy',
            firstDay: 1,
            isRTL: false,
            showMonthAfterYear: false,
            yearSuffix: ''
        };

        $.datepicker.setDefaults($.datepicker.regional['nl']);
        $.datepicker.setDefaults({ dateFormat: 'dd-mm-yy' });
    });

In the EditorFor field the date is presented dd-mm-yyyy The datepicker is format correctly in dd-mm-yy After parsing the date from the datepicker, dateformat is suddenly mm/dd/yyyy

1 Answer 1

3

Found out the issue... I forgot I used a shared template for date :-/

@Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "datepicker" })


<script type="text/javascript">
    $(document).ready(function () {
        $('.datepicker').datepicker({ dateFormat: "dd-mm-yy" });
    });
</script>

regards, Roy

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

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.