Counting experience in Months – Javascript

I am writing a function that will count experience based on 2 variables: startDate, EndDate that has following format:

First position:  2013-12-01 to 2015-12-01
First position:  2014-12-01 to 2015-05-01 
First position:  2007-07-01 to 2011-10-01

Right now I am wondering how to actually achieve that and it is a little bit tricky for me, because I need tell somehow how to actually counts the months, how many mounths got 1 years (12) and do proper math but I don’t know how to do it good way with Javascript

I could transfer variables startDate, endDate to have only YEAR-MONTH and then write some logic that will count the experience in Months or maybe there is any library that could help with that?

Here is the code that I write so far

async function dupa(){

fileToCurate.forEach(candidate => {
    candidate.folder.forEach(folder => {

        function countFirstExp(firstExperienceStartDate, firstExperienceEndDate){
            // assign variables From, To and count experience
            var firstExperienceStartDate = folder.positions[0].from;
            var firstExperienceEndDate = folder.positions[0].to;            
            
            // if startDate or endDate is not null we can count exp
            if(firstExperienceStartDate !== null && firstExperienceEndDate !== null){
                
                let startDate = firstExperienceStartDate['$date'].substr(0, 10)
                let endDate = firstExperienceEndDate['$date'].substr(0, 10)
                console.log("First position: ", startDate, "to", endDate)
            }
        }
        countFirstExp();
        
        //console.log(candidate.folder)
    });
     //console.log(candidate);
});

}

1 thought on “Counting experience in Months – Javascript”

  1. let from = new Date('2007-07-01')
    let to = new Date('2011-10-01')
    
    // difference in miliseconds
    const difference =  new Date(to - from).getTime() 
    
    // aprrox value of month in seconds
    const monthInmiliseconds = 2590000000 
    
    
    console.log( 'experience :', Math.floor(difference / monthInmiliseconds), 'months')
    
    
    Reply

Leave a Comment