-3

Lets say that i have two dates:

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

and the second date is a file creation date :

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

i want to do something like: Date.Now - FileDate.

this is my code:

Dim ddd As TimeSpan = Date.Now - SecondDate

there is no "Year" Property in the timespan. (the Year Property Should Be 2 2013 - 2011 = 2 , but there is a days property - but i have to get the year/s and the month/s)

Update:

thanks everyone , but i created a function:

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

Hope its helped someone.

ETTT
  • 1
  • 2
  • 7
    How long is a year if you don't have a date? It's a meaningless measure that can't be accurate by definition. Same with months. – spender Aug 10 '13 at 20:09
  • 1
    Please decide if 365 days is one year or less, than it is possible to answer. – Alexei Levenkov Aug 10 '13 at 20:09
  • Based on the question, you want the difference between 31-12-2012 and 1-1-2013 to be -30 days, -11 months, +1 year. That doesn't look right. Are you sure you want that instead of just +1 day? –  Aug 10 '13 at 20:09
  • Looking at the question as described, I would have said the accuracy of a year is over-thinking. If one is trying to see how old a file is, a rough guide of years would be close enough. (Obviously there are plenty of scenarios where hight accuracy is needed as well) – CodeBeard Aug 10 '13 at 20:14
  • You could ignore the TimeSpan altogether and calculate the difference from the two dates: http://techbrij.com/convert-timespan-to-year-month-date-age-calculation-in-net – Hamish Smith Aug 10 '13 at 20:33
  • I think my answer is more "accurate" and more performant than your last update code. You only need to add the "days" part. – pspet Aug 11 '13 at 01:05

5 Answers5

1

You can use DateTime.Parse("date as a string") to convert both into DateTime object. You will then want to use DateTime.Subtract http://msdn.microsoft.com/en-us/library/8ysw4sby.aspx Then you will have the TimeSpan you want which you can explore here: http://msdn.microsoft.com/en-us/library/system.timespan_methods.aspx The TotalDays property can be divided by 365 to get years (please note the fair challenge to this below). The calculation for months would be more complex.

CodeBeard
  • 515
  • 3
  • 10
  • 2
    What about leap years? – It'sNotALie. Aug 10 '13 at 20:07
  • A fair point. I would have said that, similarly, with months you will want to make a decision about the accuracy needed. In some cases 365 and 30 will be acceptable. In other cases you will want to take all the details into consideration. – CodeBeard Aug 10 '13 at 20:09
1

C# code:

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

You cannot get "years" or "months" because not all years or months have the same day count.

Alternative:

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;
}
pspet
  • 184
  • 3
0
Dim ddd As New DateTime((Date.Now - SecondDate).Ticks)

Alternatively, there is a function for that in Jon Skeet's Noda Time library.

It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
0

There's no year in TimeSpan since the TimeSpan structure is simple and a year depends on the culture and the start and end point. This should work:

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  

You could also have a look at Noda-Time of Jon Skeet which supports that.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

The problem is that there is no real common definition of a year. In the calendar, a year is 365 or 366 days long. In banking, a year is 360 days long, etc.

From your code I'd say you want the difference in the calender's years so you can't do that from a timespan (as the timespan doesn't know if a year is 365 or 366 days long).

So what you could do is this:

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);
Manuel Schweigert
  • 4,884
  • 4
  • 20
  • 32