开发者

DateTime Formatting in repeater

开发者 https://www.devze.com 2022-12-29 21:41 出处:网络
I am trying to format the date time (yyyy/MM/dd) in a repeater which is binded to an ObjectDataSource as shown

I am trying to format the date time (yyyy/MM/dd) in a repeater which is binded to an ObjectDataSource as shown

(THIS WORKS)

开发者_如何学Go <%# ((MyType)Container.DataItem).CreateDateTime.ToString("yyyy/MM/dd")%>

(THIS DOES`NT WORKS)

<%# String.Format("{0:yyyy/MM/dd}",((MyType)Container.DataItem).UpdateDateTime)%>

I want to have both things working because sometimes the property UpdateDateTime is null, in such cases the second line of code handles smart.

Thank you for the help in advance.


I just tested you second line of code that you say doesn't work and it works fine when UpdateDateTime is null assuming that it's datatype is datetime?.

The your first line of code will in fact error out if CreateDateTime is null and that can be fixed by just doing it like your second line:

<%# String.Format("{0:yyyy/MM/dd}",((MyType)Container.DataItem).CreateDateTime) %>

Can you provide more information as to what the datatype is and what error you are receiving?

Another solution would be to use (again assuming DateTime? datatype but you could check for DBNull as well):

<%# (((MyType)Container.DataItem).UpdateDateTime == null) ?
    "No Date Text" :
    ((MyType)Container.DataItem).UpdateDateTime.Value.ToString("yyyy/MM/dd") %>
0

精彩评论

暂无评论...
验证码 换一张
取 消