5

Assume that a model has a datetime datatype. So in view there will be a blank field ask you to input datetime.

Is there a way to fill this HTML field with today's date/datetime as default value?

1
  • 1
    Please define today. Is it today in Australia, today in USA, today in France, today at the client browser, today where the server is hosted? Commented Jan 23, 2011 at 8:51

4 Answers 4

6
model.SomeDateField = DateTime.Now(); return View(model); 
Sign up to request clarification or add additional context in comments.

5 Comments

That would fill in the server's current date/time. If he wants it to default to the time zone that the browser is in, he would need to use some javascript/jquery magic
The question didn't ask about accounting for time zone differences.
well you don't have to prepolute it in the view for this to work. And Now is a property not a method.
I meant you don't have to prepopulate it in the controller for this to work...actually you shouldn't.
It is better to keep the model and controller separate. SomeDateField is better set in the model. 'return View(model);' belongs in the controller.
3

Simple

<%: Html.TextBox("date", DateTime.Now.ToShortDateString()) %> 

Or use javascript to get the client's browser date. Better yet use jquery's datepicker for nice UI for selecting dates. With it you can also prepopulate the default date:

/** Enable jquery UI datepickers **/ $(document).ready(function () { $(function () { $(".date-select").datepicker({ dateFormat: 'dd.mm.yy' }); $(".date-select").datepicker($.datepicker.regional['sl']); }); $(function () { $("#someDateField").datepicker('setDate', new Date()); }); }); 

Comments

0

Wanted to add what I found that I needed for filling a datepicker with today's date that was being generated from the DateTime data type in HTML5 (via razor at the view in the value attribute) which is this:

@Html.TextBoxFor(model => model.YourDateInModel, new { @id = "YourDateId", @type = "date", @Value = DateTime.Now.ToString("yyyy'-'MM'-'dd") }) 

Note that the ToString() format was necessary to get it to show the date in the datepicker field (it requires a database format).

Source: https://forums.asp.net/p/2154640/6320474.aspx?p=True&t=637376668781782563

Edit: This ended up leaving the database blank/null by itself, so I also had to set the model with the initial value of today's date (as mentioned above).

public DateTime YourDateInModel { get; set; } = DateTime.Now; 

In my case I needed both. It may also be helpful to use something like:

$("#YourDateId").prop("disabled", true); 

If you are using JQuery/Javascript to prevent changes to the field where applicable.

Comments

-1

You can use simple javascript for that.

Put this code in the header:

 <script> function setToCurrentDate(inputElement) { var currentDate = new Date().toISOString().split('T')[0]; inputElement.value = currentDate; } </script> 

Add a body tag to the document and set its onload like this:

<body onload="setToCurrentDate(document.getElementById('dataInput'));"> 

*Do not forget to close the body tag at your page in the proper place.

And at last, set the onfocus of your date control with: "setToCurrentDate(this)"

<input class="form-control" id="dataInput" type="date" data-val="true" data-val-required="this field is mandatory." name="Data" onfocus="setToCurrentDate(this)" /> <input name="__Invariant" type="hidden" value="Data" /> 

It worked very fine for me.

1 Comment

Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.