# Calculating Length of Service from days to Years/Months/Days

Copper Contributor

# Calculating Length of Service from days to Years/Months/Days

I am trying to calculate employees Length of Service from Total days to Years/Months/Days.  What formula(s) would I use for that type of calculation?

 05/27/24 Length of Service Start Date Days Years Years Months Days 10/29/18 2037 5.58 5 07/17/17 2506 6.87 6 09/07/10 5011 13.73 13 01/05/21 1238 3.39 3 12/09/22 535 1.47 1 12/01/16 2734 7.49 7 01/20/21 1223 3.35 3 11/02/22 572 1.57 1 10/16/23 224 0.61 0 08/23/04 7217 19.77 19 05/22/17 2562 7.02 7 01/03/32 33748 92.46 92 01/14/09 5612 15.38 15 09/07/10 5011 13.73 13 05/20/24 7 0.02 0 05/20/24 7 0.02 0 09/06/11 4647 12.73 12 11/04/08 5683 15.57 15 01/05/21 1238 3.39 3 01/24/23 489 1.34 1 07/24/17 2499 6.85 6 09/13/21 987 2.7 2 05/29/12 4381 12. 12 04/12/21 1141 3.13 3

I greatly appreciate any assistance. Kristin

4 Replies

# Re: Calculating Length of Service from days to Years/Months/Days

Thank you very much.

# Re: Calculating Length of Service from days to Years/Months/Days

@Kristin_Lyons depending on what are your accuracy requirements. Your need to choose between two calculations:

• Treat every month as a 30 days month, ignoring your Start Date field and leap years (every year will be 365 days long). This approach simpliflies the calculations but is not exact.
• Start to count from your Start Date field, filling every month and taking into account leap years in order to correctly fill February. This approach is a little more complex.

Regardless of your choice, you don't need the first decimal Years field.

Hope this helps,

Miguel.

# Re: Calculating Length of Service from days to Years/Months/Days

@mfcorral accuracy is what I need. Can you give me an example formula? I am thinking thatvwill become a long formula, like the 12 year calculation. Trying to understand what the formula would look like.

Kristin

# Re: Calculating Length of Service from days to Years/Months/Days

Hi again @Kristin_Lyons, more than a formula you need and algorithm. In C#:

``````using System;

public class Program
{
public static void Main()
{
var startDate = new DateTime(2012, 5, 21);
var totalDays = 2102;
var span = GetDateTimeSpan(startDate, totalDays);

Console.WriteLine(\$"Years: {span.Years}");
Console.WriteLine(\$"Months: {span.Months}");
Console.WriteLine(\$"Days: {span.Days}");
}

public static (int Years, int Months, int Days) GetDateTimeSpan(DateTime startDate, int totalDays)
{
int years = 0;
int months = 0;
int days = 0;

var currentDate = endDate;

// If totalDays is negative, swap dates
if (startDate > endDate)
{
endDate = startDate;
startDate = currentDate;
}

// Count years
currentDate = startDate;
while (currentDate.AddYears(years + 1) <= endDate)
{
years++;
}

// Count months
while (currentDate.AddMonths(months + 1) <= endDate)
{
months++;
}

// Count days
while (currentDate.AddDays(days + 1) <= endDate)
{
days++;
}

return (years, months, days);
}
}``````

You can play with it at https://dotnetfiddle.net/. Be sure that you choose the following options:

• Language: C#
• Project type: Console
• Compiler: .NET 8

Replace all the code, and you got it.

Regards,

Miguel.