15

I have a Textbox with type as date. I am trying to set default value of the textbox to current date.

@Html.TextBoxFor(x => x.Date, new { @id = "Date", @type = "date", 
                                    @value = DateTime.Now.ToShortDateString() })

The above line doesn't set default value. How to set default value as current date?

1
  • 1
    Just set the value of Date in your model before passing it to the view.
    – user3559349
    Commented Oct 17, 2014 at 4:56

8 Answers 8

38

As Stephen Muecke said, you need to set the property's value on the model.

// in controller method that returns the view.
MyModel model = new MyModel();
model.Date = DateTime.Today;

return View(model);

And your Razor would be:

@Html.TextBoxFor(x => x.Date, "{0:yyyy-MM-dd}", new { @class = "form-control", @type = "date"})

Note that the id and the name properties should be automatically assigned to the property name when using a For method, such as @Html.TextBoxFor(), so you don't need to explicitly set the id attribute.

4
  • 2
    I suspect OP may not want property Date to be a string. Instead, set the property to DateTime.Now or DateTime.Today and use @Html.TextBoxFor(m => m.Date, "{0:dd-MM-yyyy}", new { @class = "form-control"}) Only set @type = "date" if your want to render the browsers datepicker (in which case the format must be yyyy-MM-dd)
    – user3559349
    Commented Oct 17, 2014 at 5:23
  • Thanks @StephenMuecke i do wanted it in Date format...I used {0:yyyy-MM-dd} & then only it worked..as HTML5 input date expects the date in the RFC3339 format....!
    – Anup
    Commented Oct 17, 2014 at 6:20
  • @Anup, Just be aware that IE has some problems using this so worth testing in all browsers.
    – user3559349
    Commented Oct 17, 2014 at 6:36
  • @StephenMuecke, I always prefer data to be a string when sending it to the view. All formatting happens in the controller so very minimal logic exists in the view.
    – ps2goat
    Commented Oct 17, 2014 at 13:35
31

It's better way to manage in view

@Html.TextBoxFor(x=> x.Date, new { @Value = @DateTime.Now.ToShortDateString() })
3
  • Welcome to StackOverflow. Thanks for your answer. It's best to format code, in the way you can see I've done, for ease of reading. Commented Oct 17, 2014 at 5:30
  • Works perfect for what I need
    – Scanner
    Commented Jan 26, 2016 at 11:56
  • 2
    This should be the accepted answer. straight to the point and easier to implement.
    – CyberNinja
    Commented Jun 26, 2017 at 13:19
5

<input asp-for="date" value="@DateTime.Today" type="datetime" class="form-control" />

This works for me. Remember to change 'date' according to your model.

and use this, if you need time as well

<input asp-for="date" value="@DateTime.Now" type="datetime" class="form-control" />

2

in your controller use this:

MyModel model = new MyModel();
model.Date = model.date == null ? DateTime.Today : model.date;

return View(model);

in your view page

@Html.TextBoxFor(x => x.Date, "{0:yyyy-MM-dd}",
 new { 
   @class = "form-control", 
   type = "date"
})
1
  • Great solution. This was the only thing that worked for me. FYI, I didn't have to do the controller stuff. Commented Jan 26, 2023 at 23:28
1

Another Solution:

 @Html.TextBoxFor(model=>model.CreatedOn, new{@value= System.DateTime.Now})

It works on my end, sure it will work on yours.

1
 $(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);
    });
1
  • It's always better to do any processing on the server side though. Commented Jan 15, 2019 at 14:51
0

you could use this script in your view

An your razor your razor page would be like

    window.onload = function () {
        var date = new Date();

        var Mes = String(date.getMonth() + 1).padStart(2, '0');
        var Dia = String(date.getDate()).padStart(2, '0');
        var Anio = date.getFullYear();
        document.getElementById("currentDate").value = Anio + "-" + Mes + "-" + Dia;
    };
<div class="col-md-10">
                <input class="form-control text-box single-line"  id="currentDate" name="currentDate" type="date" value="">
 </div>

0

window.onload = function () { var date = new Date();

    var Mes = String(date.getMonth() + 1).padStart(2, '0');
    var Dia = String(date.getDate()).padStart(2, '0');
    var Anio = date.getFullYear();
    document.getElementById("currentDate").value = Anio + "-" + Mes + "-" + Dia;
};
1
  • Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation? Commented Mar 8 at 0:47

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