ASP.NET Parse DateTime result from ajax call to javascript date

后端 未结 4 1450
情书的邮戳
情书的邮戳 2020-12-01 18:42

Introduction:

I have a WebMethod on my ASP.NET page which returns a Person object. One of the fields is Birthday

4条回答
  •  佛祖请我去吃肉
    2020-12-01 19:20

    Another way of tackling this problem is to make a new element in your person class and then use that. Example

    public class Person {
       public int Id {get;set;}
       public string Name {get;set;}
       public DateTime Birthday {get;set;}
       public string BirthdayFormat { get {
          return Birthday.toString("dd/MM/YYYY")
       }}
    }
    

    I would have thought this would be the best way as then all the formating is in one place and where possible you can use. [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/YYYY}")]

    Above the Birthday element, so that displayFor will use the correct formatting.

提交回复
热议问题