如何从另一个日期中减去一个日期?

时间:2013-08-10 20:01:47

标签: c# vb.net winforms

让我们说我有两个日期:

Date.Now:#8/10/2013 10:53:46 PM#

,第二个日期是文件创建日期:

#7/10/2011 9:57:58 PM#

我想做的事情如下:Date.Now - FileDate

这是我的代码:

Dim ddd As TimeSpan = Date.Now - SecondDate

在时间跨度内没有“年”属性。 (年份属性应为2 2013 - 2011 = 2,但有一天属性 - 但我必须得到年/月和月/ s)

更新

感谢大家,但我创建了一个功能:

Public Class Timee
        Private _Days As Integer
        Public Property Days() As Integer
            Get
                Return _Days
            End Get
            Set(ByVal value As Integer)
                _Days = value
            End Set
        End Property
        Private _Months As Integer
        Public Property Months() As Integer
            Get
                Return _Months
            End Get
            Set(ByVal value As Integer)
                _Months = value
            End Set
        End Property
        Private _Years As Integer
        Public Property Years() As Integer
            Get
                Return _Years
            End Get
            Set(ByVal value As Integer)
                _Years = value
            End Set
        End Property
    End Class
    Public Function GetTimeBetween(ByVal datee As Date, ByVal datee2 As Date) As Timee
        Dim tt As TimeSpan = datee - datee2
        Dim dd11 As Integer = tt.Days
        Dim bb1 As Integer
        Dim Month As Integer
        Dim Years1 As Integer
        Do Until dd11 <= 0
            bb1 = dd11
            dd11 = dd11 - 365
            Years1 = Years1 + 1
        Loop
        Years1 = Years1 - 1
        Dim Dayss As Integer
        Do Until bb1 <= 0
            Dayss = bb1
            bb1 = bb1 - 30
            Month = Month + 1
        Loop
        Dim tt1 As New Timee
        tt1.Days = Dayss
        tt1.Months = Month
        IF Years1 < 0 Then
           Years1 = 0
        End IF
        tt1.Years = Years1
        Return tt1
    End Function

希望它帮助了某人。

5 个答案:

答案 0 :(得分:1)

您可以使用DateTime.Parse(“date as a string”)将两者转换为DateTime对象。 然后,您将需要使用DateTime.Subtract http://msdn.microsoft.com/en-us/library/8ysw4sby.aspx 然后你将拥有你想要的TimeSpan,你可以在这里探索:http://msdn.microsoft.com/en-us/library/system.timespan_methods.aspx TotalDays属性可以除以365来获得年份(请注意下面的公平挑战)。 几个月的计算会更复杂。

答案 1 :(得分:1)

C#代码:

DateTime secondDate = ...;
var ddd = DateTime.Now - secondDate;
var days = ddd.TotalDays;

您不能获得“年”或“月”,因为并非所有年份或月份都有相同的日期。

替代:

var now = DateTime.Now;
var totalYears = now.Year - secondDate.Year;

if (secondDate.Month > now.Month || (secondDate.Month == now.Month && secondDate.Day > now.Day))
{
    totalYears--;
}

var months = now.Month - secondDate.Mont;

if (secondDate.Day > now.Day)
{
    months--;
}

if (months < 0)
{
    months = 12 + months;
}

答案 2 :(得分:0)

Dim ddd As New DateTime((Date.Now - SecondDate).Ticks)

或者,在Jon Skeet的Noda Time库中有一个函数。

答案 3 :(得分:0)

TimeSpan中没有年份,因为TimeSpan结构很简单,一年取决于文化以及起点和终点。这应该有效:

Dim zeroTime As New DateTime(1, 1, 1)
Dim a = #8/10/2013 10:53:46 PM#
Dim b = #7/10/2011 9:57:58 PM#

Dim span As TimeSpan = a - b
Dim years As Integer = (zeroTime + span).Year - 1  

您还可以查看支持该功能的Jon Skeet的Noda-Time

答案 4 :(得分:0)

问题是一年没有真正的共同定义。在日历中,一年是365或366天。在银行业,一年是360天等等。

从你的代码中我会说你想要在日历年中有所不同,所以你不能从时间跨度上做到这一点(因为时间跨度不知道一年是365天还是366天)。

所以你能做的就是:

var years = DateTime.Now.Year - fileDate.Year;

var dayInCurrentYear = fileDate.AddYears(years);

//if the day is later in the year than today's day go back one year
if(dayInCurrentYear > DateTime.Now)
{
   years--;
   dayInCurrentYear = dayInCurrentYear.AddYears(-1);
}

var span = DateTime.Now - dayInCurrentYear;

Console.WriteLine("File was created {0} years and {1} days ago", years, span.TotalDays);