gpt4 book ai didi

c# - 如何检查对象 StartDate 是否在同一小时内

转载 作者:太空宇宙 更新时间:2023-11-03 20:10:27 24 4
gpt4 key购买 nike

我的 razor View 中有以下代码,如果对象的 StartDate 等于当前时间(在一小时内添加),我需要显示一个单词“New”:-

我尝试了以下方法,但即使刚刚添加了对象,也不会显示“新建”一词:-

@if (item.StartDate.Value.Date == DateTime.Today)
{
@item.StartDate.Value.ToString("h:mm tt")</span>
<span class="green b">
If (item.StartDate.Value.TimeOfDay.Hours ==
DateTime.Today.TimeOfDay.Hours)
{
New !
}
</span>
}

最佳答案

如果你想检查它是否在一小时内,那么下面的方法应该适合你:

if (DateTime.Now - StartDate < TimeSpan.FromHours(1))

这假设 StartDate 在创建时已设置为 DateTime.Now 而不是 DateTime.Today,因为这将为您提供 00:00:00一天中的时间。

例如:

<span>@item.StartDate.ToString("h:mm tt")</span>
@if (DateTime.Now - StartDate < TimeSpan.FromHours(1))
{
<span class="green b">
New !
</span>
}

关于c# - 如何检查对象 StartDate 是否在同一小时内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20425202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com