问题描述
我有一个将日期显示为dd / mm / yyyy的数据库在我的列表视图中,我已对其进行了更改,因此它显示为mm / dd / yyyy
I have a database which displays dates as dd/mm/yyyy In my listview I have changed it so it displays as mm/dd/yyyy
<asp:Label ID="TPTIMEIN" runat="Server" Text='<%#Eval("TPTIMEIN", "{0: MM/dd/yyyy HH:mm:ss}") %>' />
我还有另一部分代码,如果日期超过2小时,它将字体颜色更改为红色old
I have another part of code which changes the font color to red if the date is more than 2 hours old
Label TimeLabel = (Label)e.Item.FindControl("TPTIMEIN");
if (TimeLabel != null)
{
DateTime Total;
if (DateTime.TryParse(TimeLabel.Text, out Total) == true)
{
if (Total < DateTime.Now.AddHours(-2))
{
TimeLabel.ForeColor = System.Drawing.Color.Red;
}
}
}
但是,这是问题所在上面的代码似乎只适用于旧格式dd / mm / yyyy。因此它将突出显示01/11 / yyyyy,但不会突出显示01/14 / yyyyy,因为它无法识别。我将如何更改?
However, here's the problem the code above only seems to work on the old format dd/mm/yyyy. So it will highlight 01/11/yyyy but not 01/14/yyyy as it's not recognizing it. How would i change this?
希望这是有道理的.....
Hope this makes sense.....
编辑
我已经尝试过类似的操作,但是不能使用<这样
I've tried something like this but I can't use a "<" this way
if (Total < DateTime.Now.AddHours(-2).ToString("MM.dd.yyyy"))
推荐答案
因为您已经知道要使用的格式使用您可以直接使用DateTime.TryParseExact。
Since you already know the format you want to use you can just use DateTime.TryParseExact instead.
更改
if (DateTime.TryParse(TimeLabel.Text, out Total) == true)
到
if(DateTime.TryParseExact(TimeLabel.Text,"MM/dd/yyyy HH:mm:ss",null, DateTimeStyles.None, out Total) == true)
这篇关于C#ASP.Net日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!