How to set DateTime to a default value of now in MVC

Home > Search > How-to
  by

Lets say your SQL database contains both date_published and date_updated columns. When navigating to www.example.com/App/Details/1, the page should display the values of the date_published and date_updated columns from SQL. This can be accomplished by adding the following markup to the /Models/Example.cs file.

[Required]
public DateTime date_published { get; set; }

[Required]
public DateTime date_updated { get; set; }

 

Ensure the /View/Example/Details file has the following HTML helpers.

@Html.DisplayFor(model => model.date_published)
@Html.DisplayFor(model => model.date_updated)

 

By default, the www.example.com/App/Details/1 page will display both the date and time, such as 01/01/2016 12:00 AM. To modify the format of the DateTime, add using System.ComponentModel.DataAnnotations; to the very beginning of the /Models/Example.cs file

using System.ComponentModel.DataAnnotations;

 

Also add [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)] before the public DateTime class. This will display only the Date, such as 01/01/2016.

[Required]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime date_published { get; set; }

[Required]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime date_updated { get; set; }

 

When navigating to www.example.com/App/Edit/1, editing the record may update date_published and date_updated to be 01-01-0001. We want date_published to retain the DateTime in SQL, and we want date_updated to be updated to the current DateTime when the record is updated. We can add a HiddenFor HTML helper to the /View/Example/Edit file in MVC for the date_published and date_updated columns. We set the value of the date_updated to System.DateTime.Now.

@Html.HiddenFor(model => model.date_published)
@Html.HiddenFor(model => model.date_updated, new { @Value=System.DateTime.Now })

 

Viewing the source of www.example.com/App/Edit/1 will include hidden HTML tags for both the date_published and date_updated columns. This is kind of a messy solution, as both hidden tags contain a value for the DateTime from SQL. Only the date_updated tag contains 2 values, one for the DateTime from SQL and another for the System.DateTime.Now. Although this is a messy solution, it achieves the goal. The date_published will retain the DateTime from SQL, and date_updated will update to the current DateTime.

<input id="date_published" name="date_published" type="hidden" value="7/01/2016 1:23:27 PM" />
<input Value="08/13/2016 13:50:42" id="date_updated" name="date_updated" type="hidden" value="7/01/2016 1:23:27 AM" />

 


It is also possible to change the behavior of date_published and date_updated in the /Controllers/Example.cs file.

  • private DateTime _date; creates a private DateTime class named _date
  • In the public DateTime date_updated class, set contains _date = DateTime.Now updates date_updated to the current date when a record is displayed and updated.
private DateTime _date;

[Required]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime date_published { get; set; }

[Required]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime date_updated {
    get {
         return _date;
        }
    set {
         _date = DateTime.Now;
        }
    }

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments