Set DataFormatString for MVC3 DateTime

后端 未结 2 1740
孤街浪徒
孤街浪徒 2020-12-20 13:44

I have some issue with displaying date correctly, using the MVC3 DateTime object.

In the controller, I set Date = DateTime.Now

相关标签:
2条回答
  • 2020-12-20 14:26

    Your viewModel correct (only thing is you need to use "yy" if you want to show short year in C#), but I don't understand what do you do with this code

    @Html.LabelFor(m => Model.Date, "Date")
    @Html.ValidationMessageFor(m => Model.Date) 
    @Html.TextBoxFor(m => Model.DatoForIntervju, new { @class = "datepicker" })
    

    I show you how I add datepicker, in this example date shows and return from view in correct format:

        @Html.EditorFor(m => m.Date) 
        @Html.ValidationMessageFor(m => m.Date)
    
        <script type="text/javascript">
            $(document).ready(function () {
                $('#Date').datepicker({ firstDay: 1, dateFormat: 'dd.mm.yy', clickInput: true });
            });
        </script>
    
    0 讨论(0)
  • 2020-12-20 14:46

    change

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.mm.yyyy}")]
    

    to

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yy}")]
    

    Then, instead of textboxfor, use

    @Html.EditorFor(m => m.Date)
    
    0 讨论(0)
提交回复
热议问题