简体   繁体   中英

How to convert a date time in a ASPX/MVC4 page?

I have a MVC4 application by using ASPX. In the controller I use LINQ. I read a string and I convert the value as a datetime value. Everything looks okay. The datetime notation is: "dd-MM-yyyy".

But when I see my web page I see date values like 30-07-2001 as 01-01-0001. Why can't the page show the value as "30-07-2001" in stead of "01-01-0001"?

The data-type in the model is DateTime. Then it didn't mind if the datetime notation is European or American style?!

Can somebody help me how to solve this?

In the controller

 myModel.ID = myRecord.ID;
 DateTime datum = DateTime.ParseExact(myRecord.Datum, "dd-MM-yyyy", null);
 myModel.Datum = datum;

In the aspx-page

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<myWeb.Models.myModel>>" %>

    if (Model.Count() > 0) {  %>
    <table>
        <tr>
            <th width="75" align="right" valign="top">Datum</th>
...
            <td><%: String.Format("{0:dd-MM-yyyy}", item.Datum)%></td>

and the model

  public DateTime Datum { get; set; }

I have put the CultureInfo in global.asax.

In the event:

protected void Application_Start()

... and that works.

Thanks for your commands.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM