简体   繁体   中英

Date Formats in MVC

My view models and controllers seem to save the date time in a different format. For instance, in my view in the HTML text box, if I had entered in 07/12/2015, it would have saved the value as if it is "December 7, 2015". I am using the Boostrap Javascript DateTime Picker. (a side note: if I entered in the "dateFormat" for the datetime picker, the picker would not work, and that's why I commented out that line)

Is there a way such that I can change the saved format of the HTML text box?

My view model: (part)

 <div class="form-group">
@Html.Label("Actual Completion Date", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Actual_Completion_Date, new { htmlAttributes = new { @Value = Model.Actual_Completion_Date, @class = "form-control" } })
            </div>
        @Html.ValidationMessageFor(model => model.Actual_Completion_Date, "", new { @class = "text-danger" })
    </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>

    </div>
}

@section scripts {
    <script type="text/javascript">
        $(function () {
            $('#Actual_Completion_Date').datetimepicker({
                //dateFormat: "dd/mm/yyyy",
                defaultDate: '@Model.Actual_Completion_Date',
                format: 'L',
                showClose: true,
                showClear: true,
                toolbarPlacement: 'top',
            });

            $('#Actual_Completion_Date').datetimepicker({
                defaultDate: '@Model.Actual_Completion_Date',
                format: 'LT',
                showClose: true,
                showClear: true,
                toolbarPlacement: 'top',
                stepping: 15,
            });
        });
    </script>
}

My model (part):

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

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

Any advice or pointers? Many thanks in advance.

How datetime is parsed depends on the current language for posted values and values passed as querystring.

Values passed as part of the route are parsed using invariant culture. Check with f12 tools or Fiddler what kind of request you have.

If you want to change the behaviour you would change the curent culture for the thread (for posted values and querystrings) or register your own midelbinder (for route values)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM