You must Sign In to post a response.
  • Category: ASP.Net MVC

    How to format date in MVC

    I have a table tblYear_Mast.Three columns are present in the table i.e. Year_Code,Year_From,Year_To. I am loading this data in dropdownlist.I want to display data as
    The code is as shown below

    ViewBag.Year_Code = New SelectList(db.tblYear_Mast, "Year_Code", "Year_From", tblLeave_Trans.Year_Code)
    Return View(tblLeave_Trans)
  • #765224
    do you have yearFrom and yearTo column in database, Please elaborate your question more ?
    can't you use SQL queries to accomplish your output

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765239
    You can do it in various ways.
    1. In SQL you can try CAST and CONVERT.
    2. In C# you can do it in ToString also.

    Based on your requirement you can do it.

    By Nathan
    Direction is important than speed

  • #765248
    You can use given code snippet as guideline to Format date and time in MVC 4/MVC3

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

    public class ExampleController : Controller
    public ActionResult Index()
    return View(new ExaViewModel
    year = DateTime.Now

    public ActionResult Index(ExaViewModel model)
    return View(model);

Sign In to post your comments