简体   繁体   English

DateTime.now减去数据库中的值

[英]DateTime.now subtraction with value from database

I need to be able to get the difference from the current date with the date from my database. 我需要能够获得当前日期与数据库中日期之间的差额。 If the difference is 30, I need to display a expiry date message. 如果相差30,则需要显示到期日期消息。 My code block looks as such: 我的代码块如下所示:

var expiryDate = DateTime.Now - DateTime.Parse(user[3]);

The thing is, this returns some weird numbers which I can't seem to manage. 问题是,这返回了一些我似乎无法管理的怪异数字。 How would I go about getting just the number of days and then check if it is 30? 我该如何只获取天数,然后检查是否为30天?

Thanks for having a look guys! 谢谢大家的光临!

DateTime dt1 = DateTime.Now;
DateTime dt2 = DateTime.Parse(user[3]);

TimeSpan ts = dt1 - dt2;

int days = ts.Days;

if (days == 30){
   //do something
}
(DateTime.Now - DateTime.Parse(user[3])).TotalDays //this will give you the days.
var timespan = DateTime.Now - DateTime.Parse(user[3]);
var days = timespan.Days;
System.TimeSpan diffResult = dt1  - dt2;

if(diffResult < 0)
{
   //your code
}

When you subtract two DateTime instances you get a Timespan . 当减去两个DateTime实例时,将得到一个Timespan

Validate your value against the TotalDays property of this timespan 根据该时间跨度的TotalDays属性验证您的值

var expiryDate = DateTime.Now - DateTime.Parse(user[3]);
expiryDate.TotalDays > 30 // check in this fashion

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

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