用Repeater绑定数据时,如何在时间字段那加个判断,如果时间小于当前系统时间3天,则日期用红色字表示。 ...
程序员文章站
2022-06-07 19:41:47
...
#region// 自定义判断时间和头像 protected void rpComment_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer) { //判断时间间隔 Literal ltTime = e.Item.FindControl("lteTime") as Literal; HiddenField HFtime = e.Item.FindControl("HFTime") as HiddenField; string datetime = System.DateTime.Now.ToString(); System.DateTime addTime = DateTime.Parse(HFtime.Value); System.DateTime nowTime = DateTime.Parse(datetime); System.TimeSpan timeSpan = nowTime - addTime; ConvertNum cn = new ConvertNum();//定义转换阿拉伯数字转换为中文大写 if (timeSpan.TotalDays > 3)//判断天 { ltTime.Text = "于 " +cn.NumToChn(timeSpan.TotalDays.ToString("00")) + " 天之前说:"; } } }
<asp:HiddenField ID="HFTime" Value='<%#Eval("postdatetime") %>' runat="server"/>
<asp:Literal ID="lteTime" runat="server"></asp:Literal>