9

I am using MVC4 with the Razor view engine. My controller is as follows:

public ActionResult Index()
{

    return View(new EmployeeIndexViewModel
    {

        ToDate = DateTime.Now

    });

}

My View Model is as follows:

public class EmployeeIndexViewModel
{
    [DataType(DataType.Date)]
    public DateTime ToDate { get; set; }
}

On my view I have a simple EditorFor() call:

@Html.EditorFor(model => model.ToDate)

But still get a default (masked) value in my date picker:

Empty Date Picker

QUESTION:

How can I get today's date (from controller) to display here instead?

3 Answers 3

19

My problem was not a jQuery or MVC4 problem as I had initially thought. It has to do with how the HTML5 compatible browser displays the date picker. I was passing the date to the view in the incorrect format.

I modified my ViewModel to this and now the date populates correctly:

public class EmployeeIndexViewModel
{
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime ToDate { get; set; }
}

Correctly displaying date time

2
  • One thing I would point out for this is that the DataFormatString format is different from what is actually displayed. If you were to use DataFormatString of MM/dd/yyyy then it would not work. It seems you have to use yyyy-MM-dd regardless of how your date is actually formatted.
    – Duu82
    Commented Jun 22, 2016 at 15:28
  • 2
    You passed yyyy-MM-dd in the attribute, but I see in your photo MM/dd/yyyy. What is happening? I am asking because I want to change that order in my view but I have the same problem I see in your picture. Commented Dec 12, 2017 at 3:56
3

You should do this from jQuery level:

   $( ".selector" ).datepicker( "setDate", yourDate);

For example, if you want to set the today's date:

   $( ".selector" ).datepicker( "setDate", new Date());

You can also try adding the DisplayFormat attribute in your view model class to make sure that the date picker will interpret your data format correctly:

public class EmployeeIndexViewModel
{
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime ToDate { get; set; }
}
6
  • How would you reflect this change in the .cshtml file? Would you remove the .EditorFor() call and statically insert a Text Box?
    – JSK NS
    Commented Jan 27, 2014 at 23:32
  • Try firstly with adding the attribute I suggested, it seems to be the easiest solution Commented Jan 27, 2014 at 23:36
  • I did that, Date Picker still displays as it does in the screenshot in my question.
    – JSK NS
    Commented Jan 27, 2014 at 23:36
  • Change your editor for to : @Html.EditorFor(model => model.ToDate, new { id = "toDate"}); And then $( "#toDate" ).datepicker( "setDate", new Date()); Commented Jan 27, 2014 at 23:37
  • The problem is not the jQuery, its the .EditorFor() call adding type='date' attribute to the HTML attribute.
    – JSK NS
    Commented Jan 27, 2014 at 23:42
1

U can set the Input or Input with type="date" (datetimepicker) using JQuery.

In Razor view

@Html.TextBoxFor(m => m.StartedDate, new { @id = "mydate", @type = "date", @class = "form-control" })

Here is the JS code

   $(document).ready(function () {
        var dateNewFormat, onlyDate, today = new Date();

        dateNewFormat = today.getFullYear() + '-';
        if (today.getMonth().length == 2) {

            dateNewFormat += (today.getMonth() + 1);
        }
        else {
            dateNewFormat += '0' + (today.getMonth() + 1);
        }

        onlyDate = today.getDate();
        if (onlyDate.toString().length == 2) {

            dateNewFormat += "-" + onlyDate;
        }
        else {
            dateNewFormat += '-0' + onlyDate;
        }

        $('#mydate').val(dateNewFormat);
    });

Not the answer you're looking for? Browse other questions tagged or ask your own question.