-3

假设我有两个日期:

日期。现在:#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是,但有天属性 - 但我必须得到年/秒和月/秒)

更新:

谢谢大家,但我创建了一个函数:

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

希望它对某人有所帮助。

4

5 回答 5

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;
}
于 2013-08-10T20:11:48.327 回答
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 以获得年份(请注意下面对此的公平挑战)。几个月的计算会更复杂。

于 2013-08-10T20:06:51.510 回答
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);
于 2013-08-10T20:16:01.320 回答
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

于 2013-08-10T20:11:33.293 回答
0
Dim ddd As New DateTime((Date.Now - SecondDate).Ticks)

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

于 2013-08-10T20:08:48.953 回答