Home Ask Login Register

Developers Planet

Your answer is one click away!

MVC February 2016

Date formate change when post to controller

I want to get 31-12-2015 Date formate into my controller. but in my case when i try to post my date from view to contoller that time in debug mode it show me mm-dd-yyyy(12-31-2015) now how to get my formate.

e.g when i enter this below date

1) 12-30-2016 i get into my controller

2) 30-12-2016 i dont get date into my controller.

Model

[Required(ErrorMessage = "Enter Date")]
[DisplayName("Payment Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/mm/yyyy}")]
public DateTime Date { get; set; }

View

@Html.TextBoxFor(model => model.Date,new { @class = "k-input datepickerbootstrap", @required = "required", @id = "Date", @name = "Date" })

Javascript

$('.datepickerbootstrap').datepicker({
            autoclose: true,
            format: 'dd-mm-yyyy',
        });

Answers


Alex Art. February 2016

Most probably you are using en-US culture which uses MM/dd/yyyy date format. You need to change the server culture to one that uses dd/MM/yyyy format (for example en-GB)

You may do it through web config:

<configuration>
    <system.web>
        <globalization uiCulture="en-GB" culture="en-GB" />
    </system.web>
</configuration>

EDIT

As Stephen pointed out in his comment the format that is used in datepicker configuration has to match the format that is defined by server culture so if we take en-GB culture as an example since it's date format is dd/MM/yyyy, datepicker format has to be the same:

$('.datepickerbootstrap').datepicker({
            autoclose: true,
            format: 'dd/mm/yyyy',
        }); 

Post Status

Asked in February 2016
Viewed 1,353 times
Voted 10
Answered 1 times

Search




Leave an answer


Quote of the day: live life