日期时间格式直放站日期、格式、时间、直放站

2023-09-07 00:01:59 作者:傀儡

我试图格式化日期时间( YYYY / MM / DD )的一个中继器被绑定到一个ObjectDataSource如图

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

(这工作)

<%#((的MyType)的Container.DataItem).CreateDateTime.ToString(YYYY / MM / DD)%>                             

<%# ((MyType)Container.DataItem).CreateDateTime.ToString("yyyy/MM/dd")%>

(这DOES`NT工程)

(THIS DOES`NT WORKS)

&LT;%#的String.Format({0:YYYY / MM / DD},((MyType的)的Container.DataItem).UpdateDateTime)%>

我想有两件事工作,因为有时候财产UpdateDateTime为空,在这种情况下code中的第二行手柄的智能。

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

感谢您提前帮助。

推荐答案

我只是测试你的,你说不行code二线和正常工作时, UpdateDateTime 为空假设它的数据类型是日期时间?

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?.

的code中的第一个行会实际上错误是否 CreateDateTime 为空,并且可以修复只是在做它喜欢你的第二行:

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?

另一种解决方案是使用(再假设日期时间?数据类型,但你可以检查的DBNull 和)

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") %>