Search code examples
c#asp.net-mvcsession-cookiesdatetime-format

Display only date in Session["DateOfBirth"] Asp.NET MVC


Session["DateOfBirth"] is collecting datetime from database (SQL using Entity Framework) but I only want to show date not time.

Here is my code:

View model class:

[Display(Name = "Date of Birth")]
[Required(ErrorMessage = "Date of Birth required")]
[DataType(DataType.Date)]
public string dob { get; set; }

Controller class:

Session["dob"] = umail.u_dob;

Index view:

<div class="col">
  <h6>Date of Birth</h6>
  <p class=" ">@Session["dob"]</p>
</div>

Output screen:

enter image description here


Solution

  • below code is Working in my case. use this code in controller

    Session["dob"] = Convert.ToDateTime(Session["dob"].ToString()).ToShortDateString();