Gcobani Mkontwana

Gcobani Mkontwana

  • 565
  • 1.9k
  • 406.8k

How do i display date and time in view using asp.net mvc?

Sep 13 2021 11:58 AM

How do populate date and time in asp.net mvc?

 

// controller

 public class TimeLineDashboardController : Controller
    {
        // GET: TimeLineDashboard
        public ActionResult Index()
        {

           // var userDt = DateTime.Now.ToString("MM/dd/yyyy hh:mm tt");
            var nwDt = DateTime.Now.ToShortDateString();
            ViewData["nowDt"] = nwDt;
            return View();
        }
    }

// Model

  public class Dashboard
    {

        // Map all values from the record list in TimeLine Application DB.

        [Key]
        [Display(Name = "Incident")]
        public int Incident { get; set; }

        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd HH:mm}")]
        public DateTime dateTime { get; set; }

        [Display(Name = "Resource")]
        public string Resources { get; set; }

        [Display(Name = "Description")]
        public string Description { get; set; }

        [Display(Name = "EndDate")]
        public int EndDate { get; set; }

}

// Index

<h2>TimeLine</h2>

<div style="width:100%">
    <table style="width:100%">
        <tr>
            <td style="width: 50%;">
                @ViewData["nowDt"]
            </td>

            <td style="width: 50%;">
                @ViewData["nowTm"]
            </td>
        </tr>
    </table>
</div>


Answers (1)