Get a list of people having a birthday within 1 week

Say I have an array of births

var bdates = ['1956-12-03', '1990-03-09',...] 

I will put each through a function that will return those dates that have a birthday within 7 days from today (or now?). What I have at the moment:

 var bdays = _.map(bdates, function(date) { var birthDate = new Date(date); var current = new Date(); var diff = current - birthDate; // Difference in milliseconds var sevenDayDiff = Math.ceil(diff/31557600000) - (diff/31557600000); //1000*60*60*24*365.25 if (sevenDayDiff <= 0.01916495550992) return date; else return false; }); 

The value 0.01995183087435 was determined from the number of milliseconds for 51 weeks and the division by milliseconds for 52 weeks, then one minus this ratio should be the variable "sevenDayDiff".

My JSFIDDLE , unfortunately, is not entirely clear. There are a number of errors in this. My sevenDayDiff may be the wrong value. There is also a problem with a leap year, even if I divide by 365.25. I could just do it wrong.

This happens in the web application, so the administrator can send an email to those people who have a birthday within 7 days. Any hints or suggestions are welcome.

+5
source share
1 answer

 var bdates = ['1956-12-03', '1990-03-09', '1970-02-14']; var now = moment('2015-02-10'); var birthDates = []; bdates.forEach(function(birthDate) { var birthDay = moment(birthDate).year(now.year()); var birthDayNextYear = moment(birthDate).year(now.year() + 1); var daysRemaining = Math.min(Math.abs(birthDay.diff(now, 'days')), Math.abs(birthDayNextYear.diff(now, 'days'))); if((daysRemaining >= 0) && (daysRemaining <= 7)) { birthDates.push(birthDate); } }); document.write(JSON.stringify(birthDates)); 
 <script src="http://momentjs.com/downloads/moment.min.js"></script> 
+1
source

Source: https://habr.com/ru/post/1242680/


All Articles