title: How to get current date and time in view?
body: <p>I want to get current date and time in view.</p>
  
  <p>What I have done:</p>
  
  <ol>
  <li>Created a new project in Vs 2013 community for asp.net mvc5 (c#)</li>
  <li>Added an edmx for my database. So it is database first.</li>
  <li>Added Model-View-Controller for each of my database classes using Add > new scaffolded item.</li>
  <li>VS created view and controllers for CRUD operations. </li>
  <li>Now there are two fields in a view, which represents date and time respectively.</li>
  <li>Earlier VS created simple text boxes, but i added annotations in model so it created Datepicker and TimePicker in a view.</li>
  <li>What i would like to have is current date and time in both fields.</li>
  </ol>
  
  <p>model:</p>
  
  <pre><code>[DataType(DataType.Date)]
  public Nullable&lt;System.DateTime&gt; Date { get; set; }
  [DataType(DataType.Time)]
  </code></pre>
  
  <p>view:</p>
  
  <pre><code>    &lt;div class="form-group"&gt;
      @Html.LabelFor(model =&gt; model.Date, htmlAttributes: new { @class = "control-label col-md-2"})
      &lt;div class="col-md-10"&gt;
  
          @Html.EditorFor(model =&gt; model.Date, new { htmlAttributes = new { @class = "form-control"} })
          @Html.ValidationMessageFor(model =&gt; model.Date, "", new { @class = "text-danger" })
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="form-group"&gt;
      @Html.LabelFor(model =&gt; model.Time, htmlAttributes: new { @class = "control-label col-md-2" })
      &lt;div class="col-md-10"&gt;
          @Html.EditorFor(model =&gt; model.Time, new { htmlAttributes = new { @class = "form-control" } })
          @Html.ValidationMessageFor(model =&gt; model.Time, "", new { @class = "text-danger" })
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>controller:</p>
  
  <pre><code>public ActionResult Create()
  {
      return View();
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/pfnLN.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pfnLN.jpg" alt="These are date and time picker. i would like to have actual current date and time"></a></p>
  
  <p>These are date and time picker. i would like to have actual current date and time</p>
  
  <p>What i had tried so far:</p>
  
  <p>use of <code>@value = @model.returndate = DateTime.Now;</code></p>
  
  <p>model.returndate always gives error. i tried to use in controller as well as in view.</p>
  
  <p>using <code>Viewbag.Date = DateTime.Now;</code>  and in view <code>@Value = @Viewbag.Date</code>  nothing happens. </p>