0

I have tried different approach to make a dateTime text box, this is the one that works, but the problem is I cannot set a default date.

Here's what I am working on:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]

Then I set the date today, DateTime.Now like this model.NotifyDate = DateTime.Now; enter image description here

It doesn't set the date. However, if I remove [DataType(DataType.Date)] from the model I will get: enter image description here

I will get the date, but the calendar is gone. What's the problem? Or am I using the datepicker wrong?

Here's my view:

@Html.EditorFor(m => m.NotifyDate, new { htmlAttributes = new { @class = "form-control input-sm" } })
AdorableVB
  • 1,383
  • 1
  • 13
  • 44

3 Answers3

1

You were on the right track! Defaults such as this should be set in the ViewModel
(as it appears you're doing w/ model.NotifyDate = DateTime.Now;)

The problem here appears to be that the browser is expecting the value for the generated html input element to be formatted differently -- namely, yyyy-MM-dd vs yyyy/MM/dd.
(note the use of - vs /)

In order for the browser to correctly display the date, the value must be formatted as 2019-09-23.

ex:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]

Here is a great answer to a similar question that should shed some more light on what's going on as well.

  • Still the same problem. If I remove `[DataType(DataType.Date)]` it can be set, but the calendar will not appear. – AdorableVB Sep 23 '19 at 05:22
  • @AdorableVB right, if you remove `[DataType(DataType.Date)]` the generated html `input` element will have `type=datetime` - which isn't supported - so the browser won't know to show the datepicker. I've edited my answer to include a link to another SO question/answer that should shed some additional light on what's going on. – waterfausett Sep 23 '19 at 14:46
  • I don't know how it worked today, pretty sure I was doing the same yesterday, but `"{0:yyyy-MM-dd}"` did the trick. I guess browsers only accept that format, what I'm bothered is that on display its still `MM/dd/yyyy` – AdorableVB Sep 24 '19 at 02:41
1

Since Browser is expecting the value for the generated HTML input, you either need to provide value with client-side javascript (or client-side datepicker) or bind the value to model.

You can bind the value to model and pass to view from the controller as follows.

public async Task<ActionResult> Create()
{
    var dateInfo = new DateInfo()
            {
                StartDate = DateTime.Now
            };
    return View(dateInfo);
}

Or you can assign value from client side. jQuery UI is simple and easy to implement. You can Visit .

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Pitambar Jha
  • 11
  • 1
  • 2
  • 6
1

You should try this datepicker in js.

$('.date').datepicker({
            format: "dd.mm.yyyy",
            weekStart: 1,
            clearBtn: true,
            todayBtn: "linked",
            language: "tr",
            startDate: new Date(),
            autoclose: true
        });