简体   繁体   English

如何从一系列日期范围中找到一个人的工作经验年限,工作日期可能重叠

[英]How to find the years of work experience of a person from an array of date ranges, with potentially overlapping work dates

The goal is to use Javascript to UNIQUELY calculate the total years of a person's work experience, from an array of Date Ranges of the person's work history.目标是使用 Javascript 从一个人的工作历史的日期范围数组中唯一地计算一个人的工作经验的总年数。 There are Date Ranges which overlap (meaning, the person had multiple jobs within the overlapping period).有重叠的日期范围(意味着该人在重叠期间有多个工作)。 Here is an example below and the date format is yyyy-mm-dd;下面是一个示例,日期格式为 yyyy-mm-dd;

  • 2001-02-01 to 2009-03-01 2001-02-01 至 2009-03-01
  • 2004-06-01 to 2020-08-01 2004-06-01 至 2020-08-01
  • 2005-04-01 to 2021-03-01 2005-04-01 至 2021-03-01
  • 2008-07-01 to 2016-06-01 2008-07-01 至 2016-06-01

From the date ranges above, there are overlapping work dates.在上述日期范围内,工作日期有所重叠。 The correct computation of the person's years of work experience should be 20 years.该人的工作经验年限的正确计算应该是20年。

The problem I have is to creating an algorithm that can put into account, the overlapping periods within the person's four work histories and not count them as separate years of work experience Eg just summing up the years between each of the four job dates gives 48 years WHICH IS INCORRECT (The experience was over a 20-year period).我遇到的问题是创建一个算法,该算法可以考虑该人四个工作历史中的重叠时期,而不是将它们视为单独的工作经验年例如仅总结四个工作日期之间的年数就可以得出 48 年这是不正确的(经验超过 20 年)。

 var time_diff=0, yrs_diff=0; var jobExperience = [ {date_began:'2001-02-01', date_ended:'2009-03-01'}, {date_began:'2004-06-01', date_ended:'2020-08-01'}, {date_began:'2005-04-01', date_ended:'2021-03-01'}, {date_began:'2008-07-01', date_ended:'2016-06-01'} ]; for(let i=0; i<jobExperience.length; i++){ let date_1, date_2; let began = jobExperience[i].date_began.split('-'); let ended = jobExperience[i].date_ended.split('-'); date_1 = new Date(began[1]+'/'+began[2]+'/'+began[0]); date_2 = new Date(ended[1]+'/'+ended[2]+'/'+ended[0]); time_diff += date_2.getTime() - date_1.getTime(); } yrs_diff = parseInt(time_diff/(1000 * 3600 * 24 * 365)); console.log(yrs_diff);

The snippet above only just blindly adds up the years between each record of work history (This is not correct).上面的片段只是盲目地将每条工作历史记录之间的年数相加(这是不正确的)。 Where I need some help is a clue or better still pseudocode or complete code on how to sum up the years between each record of work history but account for overlaps between work history dates and by so doing, overlapping periods are only counted once.我需要一些帮助的地方是关于如何总结每个工作历史记录之间的年份但考虑到工作历史日期之间重叠的线索或更好的伪代码或完整代码,并且通过这样做,重叠期间只计算一次。

Here's the approach I've outlined in my comment:这是我在评论中概述的方法:

For each period, turn the start and end date into a unique "month value", then add all month values of a period to a set.对于每个时期,将开始日期和结束日期转换为唯一的“月份值”,然后将一个时期的所有月份值添加到一个集合中。
The size of the set is the wanted number of months:集合的大小是想要的月数:

 const jobExperience = [ { date_began: '2001-02-01', date_ended: '2009-03-01' }, { date_began: '2004-06-01', date_ended: '2020-08-01' }, { date_began: '2005-04-01', date_ended: '2021-03-01' }, { date_began: '2008-07-01', date_ended: '2016-06-01' } ]; const months = new Set(); // convert date into unique integer month value based on year 1900 function m1900(yyyymmdd) { const [_, y, m, d] = yyyymmdd.match(/^(\d{4})-(\d{2})-(\d{2})$/).map(Number); return (y - 1900) * 12 + m; } jobExperience.forEach(job => { const m1 = m1900(job.date_began); const m2 = m1900(job.date_ended); for (let m = m1; m < m2; m++) months.add(m); }); console.log("years:", months.size / 12);

Here is a "Brute Force" approach.这是一种“蛮力”方法。

  • Extract all the unique years提取所有独特的年份
  • Sort them对它们进行排序
  • Find the difference between the latest to the oldest year找出最新年份和最早年份之间的差异

(Assuming that all the years in the dataset belong to a single field; Different fields should not be part of the same dataset) (假设数据集中的所有年份都属于一个字段;不同的字段不应属于同一数据集)

Eg: [2001, 2004, 2005, 2008, 2009, 2016, 2020, 2021] => Working for 20 years.例如:[2001, 2004, 2005, 2008, 2009, 2016, 2020, 2021] => 工作 20 年。

A caveat with this method is that it fails to account for the months .这种方法的一个警告是它无法计算months

A solution to the same would be [NOT SURE] to convert the months to a year (month/12) and adding the same to the preceding year (Like Feb 2001 => 2001.166).相同的解决方案是 [不确定] 将月份转换为一年(月/12)并将其添加到前一年(如 2001 年 2 月 => 2001.166)。

Another caveat is that it fails to account for any break taken in between.另一个需要注意的是,它没有考虑到中间的任何中断。 (As Pointed out in the Comments). (正如评论中指出的那样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM