C# 计算两个日期之间的差异(天数)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1607336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:19:39  来源:igfitidea点击:

Calculate difference between two dates (number of days)?

c#date

提问by leora

I see that this question has been answered for Java, JavaScript, and PHP, but not C#. So, how might one calculate the number of days between two dates in C#?

我看到JavaJavaScriptPHP已经回答了这个问题,但 C# 没有。那么,如何在 C# 中计算两个日期之间的天数?

采纳答案by Greg Beech

Assuming StartDateand EndDateare of type DateTime:

假设StartDateEndDate类型为DateTime

(EndDate - StartDate).TotalDays

回答by Vitaliy Liptchinsky

Use TimeSpan object which is the result of date substraction:

使用 TimeSpan 对象,它是日期减法的结果:

DateTime d1;
DateTime d2;
return (d1 - d2).TotalDays;

回答by pyrocumulus

I think this will do what you want:

我认为这会做你想做的:

DateTime d1 = DateTime.Now;
DateTime d2 = DateTime.Now.AddDays(-1);

TimeSpan t = d1 - d2;
double NrOfDays = t.TotalDays;

回答by Philip Wallace

DateTime xmas = new DateTime(2009, 12, 25);
double daysUntilChristmas = xmas.Subtract(DateTime.Today).TotalDays;

回答by sangeetha

protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
    DateTime d = Calendar1.SelectedDate;
    // int a;
    TextBox2.Text = d.ToShortDateString();
    string s = Convert.ToDateTime(TextBox2.Text).ToShortDateString();
    string s1 =  Convert.ToDateTime(Label7.Text).ToShortDateString();
    DateTime dt = Convert.ToDateTime(s).Date;
    DateTime dt1 = Convert.ToDateTime(s1).Date;
    if (dt <= dt1)
    {
        Response.Write("<script>alert(' Not a valid Date to extend warranty')</script>");
    }
    else
    {
        string diff = dt.Subtract(dt1).ToString();
        Response.Write(diff);
        Label18.Text = diff;
        Session["diff"] = Label18.Text;
    }
}   

回答by kingPuppy

In case someone wants numer of whole days as a double (a, bof type DateTime):

如果有人希望将整日数作为双倍(a,b类型DateTime):

 (a.Date - b.Date).TotalDays

回答by Pratyush Dhanuka

For aand bas two DateTimetypes:

对于ab作为两种DateTime类型:

DateTime d = DateTime.Now;
DateTime c = DateTime.Now;
c = d.AddDays(145);
string cc;
Console.WriteLine(d);
Console.WriteLine(c);
var t = (c - d).Days;
Console.WriteLine(t);
cc = Console.ReadLine();

回答by Muba

First declare a class that will return later:

首先声明一个稍后返回的类:

public void date()
{
    Datetime startdate;
    Datetime enddate;
    Timespan remaindate;

    startdate = DateTime.Parse(txtstartdate.Text).Date;
    enddate = DateTime.Parse(txtenddate.Text).Date;

    remaindate = enddate - startdate;

    if (remaindate != null)
    {
        lblmsg.Text = "you have left with " + remaindate.TotalDays + "days.";
    }
    else
    {
        lblmsg.Text = "correct your code again.";
    }
}

protected void btncal_Click(object sender, EventArgs e)
{
    date();
}

Use a button control to call the above class. Here is an example:

使用按钮控件调用上述类。下面是一个例子:

回答by Rohidas Kadam

You can try this

你可以试试这个

EndDate.Date.Subtract(DateTime.Now.Date).Days

回答by Darren

The top answer is correct, however if you would like only WHOLE days as an int and are happy to forgo the time component of the date then consider:

最佳答案是正确的,但是如果您只希望整整几天作为整数并且乐于放弃日期的时间部分,那么请考虑:

(EndDate.Date - StartDate.Date).Days

Again assuming StartDateand EndDateare of type DateTime.

再次假设StartDateEndDate是 类型DateTime