C# ASP.Net 中的日期差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/798674/
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-05 01:37:58  来源:igfitidea点击:

Date Difference in ASP.Net

c#.net

提问by

How do I get the datedifference in ASP.NET C#?

我如何得到date差异ASP.NET C#

E.g.:

例如:

d1= 28/04/2009 09:26:14
d2= 28/04/2009 09:28:14

DateDiff = d2 - d1

回答by pyrocumulus

I think you can do it in the following way:

我认为你可以通过以下方式做到这一点:

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

TimeSpan t = d1 - d2;

回答by Kevin Tighe

Check out TimeSpan

查看时间跨度

回答by tofi9

const string DateFormat = "dd/MM/yyyy hh:mm:ss";

DateTime d1 = DateTime.ParseExact("28/04/2009 09:26:14", DateFormat, null);
DateTime d2 = DateTime.ParseExact("28/04/2009 09:28:14", DateFormat, null);

TimeSpan dateDiff = d2 - d1;

string duration = string.Format("The time difference is: {0}", dateDiff);

回答by Dries Van Hansewijck

There is an instance method Subtracton DateTimeclass which returns a TimeSpan. See article

类上有一个实例方法SubtractDateTime它返回一个TimeSpan. 见文章

DateTime now = DateTime.Parse("2009-04-28");  
DateTime newyear = DateTime.Parse("2009-01-01");  
TimeSpan difference = now.Subtract(newyear);

回答by Ricardo Conte

            Dim d1, d2 As Date
            Dim intElapsedDays As Integer
            Dim tspDif As TimeSpan
            tspDif = d2 - d1
            intElapsedDays = tspDif.Days

′should assign values to d1 and d2

'应该给 d1 和 d2 赋值

回答by hatewise

It depends on the input format timestamp. If it is, for example, unix timestamp that the code below will enumerate period info:

这取决于输入格式时间戳。例如,如果是 unix 时间戳,下面的代码将枚举周期信息:

        int TimestampFrom = 1546336800; //2019.1.1 10:00
        int TimestampTo = 1547555400;   //2019.1.15 12:30
        DateTime unixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
        long dateFromunixTimeStampInTicks = (long)(TimestampFrom * TimeSpan.TicksPerSecond);
        DateTime dateFrom = new DateTime(unixStart.Ticks + dateFromunixTimeStampInTicks, System.DateTimeKind.Utc);
        long dateTounixTimeStampInTicks = (long)(TimestampTo * TimeSpan.TicksPerSecond);
        DateTime dateTo = new DateTime(unixStart.Ticks + dateTounixTimeStampInTicks, System.DateTimeKind.Utc);
        TimeSpan Period = dateTo - dateFrom;
        int days = Convert.ToInt32(Period.TotalDays);       //>14
        int hours = Convert.ToInt32(Period.TotalHours);     //>338
        int seconds = Convert.ToInt32(Period.TotalSeconds); //>1218600

If source date format is normal DateTime then enough simple subtraction of dates will return TimeSpan structure that contain all period info.

如果源日期格式是正常的 DateTime,那么足够简单的日期减法将返回包含所有周期信息的 TimeSpan 结构。

Answering on: "How To get Interval Between Two Datetime (Timestamp) [duplicate]" This is not a duplicate.

回答:“如何获得两个日期时间(时间戳)之间的间隔[重复]”这不是重复的。