C# 计算天数

C# 计算天数,c#,asp.net,C#,Asp.net,我想从SQL数据库中的“complaintdate”字段计算天数。 在此字段中存储用户注册投诉的日期。我想看到的投诉等待20天以上,从目前的日期 是否有其他功能或方法来计算天数? 我使用的代码是 cmd = new SqlCommand("select complaintdate from complaintregister where compstatus='Attended & Open'", con); rd = cmd.ExecuteReader(); if (rd.Read()

我想从SQL数据库中的“complaintdate”字段计算天数。 在此字段中存储用户注册投诉的日期。我想看到的投诉等待20天以上,从目前的日期

是否有其他功能或方法来计算天数? 我使用的代码是

cmd = new SqlCommand("select complaintdate from complaintregister where compstatus='Attended & Open'", con);
rd = cmd.ExecuteReader();
if (rd.Read())
{
    string s = rd["complaintdate"].ToString();
}
我在旁边展示了“s”

Response.Write(s);
其显示格式为2011年4月5日凌晨12:00:00


`

这将检索
totalDays
作为您的
投诉日期
与今天之间的天数。将
GetDateFromDatabase()
替换为您的检索方式:

DateTime complaintDate = GetDateFromDatabase();
int totalDays = (int)(DateTime.Now - complaintDate).TotalDays;
if (totalDays >= 20)
{
    // Perform your actions here. 20+ days have elapsed.
}
编辑:

我添加了以下代码以使用您在编辑中提供的代码。我假设变量
cmd
con
rd
在代码的其他地方声明

cmd = new SqlCommand("select complaintdate from complaintregister where compstatus='Attended & Open' ", con);`
rd = cmd.ExecuteReader();
if (rd.Read())
{
    string s = rd["complaintdate"].ToString();
}

DateTime complaintDate;
try
{
    complaintDate = Convert.ToDateTime(s);
}
catch
{
    Response.Write("An error occurred while trying to convert the date!");
    return;
}

int totalDays = (int)(DateTime.Now - complaintDate).TotalDays;
if (totalDays >= 20)
{
    // Perform your actions here. 20+ days have elapsed.
}

这将检索
totalDays
作为您的
complaintDate
与今天之间的天数。将
GetDateFromDatabase()
替换为您的检索方式:

DateTime complaintDate = GetDateFromDatabase();
int totalDays = (int)(DateTime.Now - complaintDate).TotalDays;
if (totalDays >= 20)
{
    // Perform your actions here. 20+ days have elapsed.
}
编辑:

我添加了以下代码以使用您在编辑中提供的代码。我假设变量
cmd
con
rd
在代码的其他地方声明

cmd = new SqlCommand("select complaintdate from complaintregister where compstatus='Attended & Open' ", con);`
rd = cmd.ExecuteReader();
if (rd.Read())
{
    string s = rd["complaintdate"].ToString();
}

DateTime complaintDate;
try
{
    complaintDate = Convert.ToDateTime(s);
}
catch
{
    Response.Write("An error occurred while trying to convert the date!");
    return;
}

int totalDays = (int)(DateTime.Now - complaintDate).TotalDays;
if (totalDays >= 20)
{
    // Perform your actions here. 20+ days have elapsed.
}

根据您的问题,我不确定您是想在数据库中还是在代码中进行比较

如果要在SQL Server中执行签入,可以使用该函数

如果要执行签入代码,可以使用
DateTime.Subtract

var complaintDate = new DateTime(2011, 9, 1);
var daysSinceComplaint = (int)DateTime.Now.Subtract(complaintDate).TotalDays;
Console.WriteLine(daysSinceComplaint);

根据您的问题,我不确定您是想在数据库中还是在代码中进行比较

如果要在SQL Server中执行签入,可以使用该函数

如果要执行签入代码,可以使用
DateTime.Subtract

var complaintDate = new DateTime(2011, 9, 1);
var daysSinceComplaint = (int)DateTime.Now.Subtract(complaintDate).TotalDays;
Console.WriteLine(daysSinceComplaint);

SQL Server中的另一个解决方案是在表中输入一个计算字段,显示经过了多少天。如果CompliantDate为Null,则将显示Null

ALTER TABLE ComplianceTable ADD
DaysLapsed  AS datediff(dd,CompliantDate,getdate())
GO

SQL Server中的另一个解决方案是在表中输入一个计算字段,显示经过了多少天。如果CompliantDate为Null,则将显示Null

ALTER TABLE ComplianceTable ADD
DaysLapsed  AS datediff(dd,CompliantDate,getdate())
GO

投诉日期的数据类型是什么???什么是你的数据库引擎??mysql??投诉日期的数据类型是什么???什么是你的数据库引擎??mysql?正在运行…但结果是155.450726453993。。。2011年4月5日12:00:00 AM格式中的日期存储我将其编辑为将
totalDays
转换为
int
,因此它返回155个不带小数的数字。要仅呈现日期,请使用:
complaintDate.ToString(“m/d/yyyy”)
itworking…但结果是155.450726453993。。。2011年4月5日12:00:00 AM格式中的日期存储我将其编辑为将
totalDays
转换为
int
,因此它返回155个不带小数的数字。要仅呈现日期,请使用:
complaintDate.ToString(“m/d/yyyy”)
注意,我的解决方案将绕过日期格式设置等,因为它将使用SQL Server日期时间框架,只返回经过的天数,所以您只需使用C#if(dayslapsed>20){DoSomething()}请注意,我的解决方案将绕过日期格式等,因为它将使用SQL Server日期时间框架,只返回经过的天数,所以您只需使用C#if(dayslapsed>20){DoSomething();}