简体   繁体   English

要从asp.net mvc中的当前日期时间减去数据库中的日期时间

[英]want to minus datetime indatabase from current Datetime in asp.net mvc

i want to minus datetime in database table from current datetime and want to compare with greater than(>) operator 我想从当前日期时间减去数据库表中的日期时间,并想与大于(>)运算符进行比较

i want to compare current datetime and datetime in the database what i want is if my date from database minus current date is greater than 30 days then i want to show my label red else default 我想比较数据库中的当前日期时间和日期时间,如果数据库中的日期减去当前日期后的日期大于30天,那么我要显示红色标签,否则为默认值

here it is what i have tried 这是我尝试过的

       <label style="@(DateTime.Now-item.NewLinen>30 ? "color:red;": "")">New Linen</label>

I am getting red line below this and showing messege 我在此下方显示红线并显示信息

operator cannot be applied to operands of type 'timespan?' 运算符不能应用于“时间跨度?”类型的操作数 and int> 和int>

please help me with this 请在这件事上给予我帮助

I would usually shift the framing about slightly, and use something like this: 我通常会稍微调整一下框架,并使用类似以下的内容:

<label style="@((item.NewLinen.HasValue && item.NewLinen.Value.AddDays(30) < DateTime.Now) ? "color:red;": "")">New Linen</label>

But it's more a matter of taste. 但这更多是一个品味问题。 If you want to use TimeSpan , you need to construct another one to compare against and make it, specifically , 30 days long, not 30 seconds or any other units (which is why direct comparison with int isn't allowed) 如果要使用TimeSpan ,则需要构造另一个与之进行比较并使其相对应, 特别是30天(而不是30秒)或任何其他单位(这就是不允许与int直接比较的原因)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM