2

I need to convert the number of days into years, months, days.

Example: A Employee Experience to be calculated as per his Date of join(DOJ) and date of Relieve(DOR). We have a DOJ, DOR and Number of Days he is worked as a employee.

Have to Calculate the How many Years and Months and Days.

Example       : DOJ = 14 Feb 2000
                DOR = 08 aug 2013
Output        : 13 Years - 5 Months - 25 Days

Thanks in Advance....

Sankar
  • 6,908
  • 2
  • 30
  • 53

3 Answers3

2

This works for me:

var dor = new DateTime(2013, 08, 08);
var doj = new DateTime(2000, 02, 14);

var totalmonths = (dor.Year - doj.Year) * 12 + dor.Month - doj.Month;
totalmonths += dor.Day < doj.Day ? -1 : 0;

var years = totalmonths / 12;
var months = totalmonths % 12;
var days = dor.Subtract(doj.AddMonths(totalmonths)).Days;
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • @PanagiotisKanavos - Yes, it does. `f = new DateTime(2004, 2, 14), t = new DateTime(2004, 3, 13)` => 28 days. `f = new DateTime(2005, 2, 14), t = new DateTime(2005, 3, 13)` => 27 days. – Enigmativity Jun 03 '15 at 07:44
  • Sorry, in fact I wonder if this is after all equivalent to [this](http://stackoverflow.com/a/9216404/134204) answer that counts years etc from start to end, only much shorter. – Panagiotis Kanavos Jun 03 '15 at 07:48
  • @PanagiotisKanavos - There is a bug in the code that you linked too. Try running that code with the dates 2020-02-29 to 2021-06-29 - it returns "1y 4m 1d", when it should be "1y 4m 0d" (which my code returns). The bug in that code appears to be due to the understanding of how `.AddMonths(1)` works when applied to `2015-01-30` - it returns `2015-02-28`. So the calc ends up being short a few days. – Enigmativity Jun 03 '15 at 09:24
1

You cannot convert a number of days into years months and days because you don't know where those days lie. For example they may span 29th Feb on a leap year.

However you already have the two dates to work with so you can calculate this value like so:

Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click

    Dim doj As Date = New Date(2000, 2, 14)
    Dim dor As Date = New Date(2013, 8, 8)

    MessageBox.Show(GetDateSpanText(doj, dor))

End Sub

Public Shared Function GetDateSpanText(fromDate As DateTime, Optional toDate As DateTime = Nothing) As String
    Try
        Dim years As Integer = 0, months As Integer = 0, days As Integer = 0
        If toDate = Nothing Then toDate = DateTime.Now

        Do Until toDate.AddYears(-1) < fromDate
            years += 1
            toDate = toDate.AddYears(-1)
        Loop

        Do Until toDate.AddMonths(-1) < fromDate
            months += 1
            toDate = toDate.AddMonths(-1)
        Loop

        Do Until toDate.AddDays(-1) < fromDate
            days += 1
            toDate = toDate.AddDays(-1)
        Loop

        Return String.Format("{0} Years {1} Months {2} Days", years, months, days)
    Catch ex As Exception
        Return "Error"
    End Try
End Function
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • Actually, you can by asking the [System.Globalization.Calendar](https://msdn.microsoft.com/en-us/library/system.globalization.calendar%28v=vs.110%29.aspx) class. You don't need to though, as you *can* calculate the difference between for the year and month parts. The leftover, days, can be calculated as a TimeSpan. This is shown in this [answer](http://stackoverflow.com/a/9216404/134204) – Panagiotis Kanavos Jun 03 '15 at 07:29
0

You may use for Years and Months calculation from months, If you have 30 months then it will return 2 Years and 6 Months by using following code.

Math.Round((double)Months/12,0) Years and Month%12 Months

Muhammad Bilal
  • 1,008
  • 13
  • 14