Converting days to years, months and days | JavaScript

/ #JavaScript


A simple function for converting days to years, months and days.

Function

function getFormatedStringFromDays(numberOfDays) {
    var years = Math.floor(numberOfDays / 365);
    var months = Math.floor(numberOfDays % 365 / 30);
    var days = Math.floor(numberOfDays % 365 % 30);

    return [years, months, days].join(':');
}

Usage

const str = getFormatedStringFromDays(400);

Result

1:1:5

Comments

No comments yet...

Add comment

Info

Please log in to comment!

Newsletter

Subscribe to my weekly newsletter. One time per week I will send you a short summary of the tutorials I have posted in the past week.