Skip to content Skip to sidebar Skip to footer

How To Calculate And Check For A Bi-weekly Date

I really need your help, Let's say my demarcation start date is: December 19, 2016 as defined by the variable x How can I write a JavaScript function, such that it will check the p

Solution 1:

You could get the number of days difference between your start date and the current date then check if that number is a multiple of 14.

functiontreatAsUTC(date) {
    var result = newDate(date);
    result.setMinutes(result.getMinutes() - result.getTimezoneOffset());
    return result;
}

functiondaysBetween(startDate, endDate) {
    var millisecondsPerDay = 24 * 60 * 60 * 1000;
    returnMath.floor((treatAsUTC(endDate) - treatAsUTC(startDate)) / millisecondsPerDay);
}

var demarcationdate = newDate("2016-12-19"), 
    today = newDate(),
    days = daysBetween(demarcationdate,today),
    daystill = 14 - days%14,
    rec = days%14==0,
    d = newDate();

d.setDate(today.getDate() + daystill);
var nextDate = (d.getDate() + "/" + (d.getMonth() + 1) + "/" + d.getFullYear());

console.log("Days diff = "+days+". Recurs today = "+rec+". Next in "+daystill+" days ("+nextDate.toString()+").");

jsFiddle

Solution 2:

If Date.now() == 1482181410856, 14 days from now will be 1482181410856 + (14 * 24 * 60 * 60 * 1000) == 1483391010856.

let y = newDate(Date.now() + (14 * 24 * 60 * 60 * 1000));
console.log(y.toUTCString()); // "Mon, 02 Jan 2017 21:03:30 GMT"

Solution 3:

Assuming you really want to compare precise dates, i.e. to the milliseconds, then:

var present_date = newDate();
if(present_date.getTime() === x.getTime()) alert("Today is the same date as x");
else {
    var y = newDate(x.getTime());
    y.setDate(y.getDate() + 14); // add 14 daysif(present_date.getTime() === y.getTime()) alert("Today is the same date as y");
}

But most of the time we want to compare dates as full days, not milliseconds, so you'd have to compare ranges instead (from midnight to 11:59PM)... In that case, I recommend using a library to make your life easier - like moment.js for instance...

Hope this helps!

Solution 4:

This is probably a duplicate of Add +1 to current date.

If you have a start date, say 20 December, 2016, you can calculate 14 days after that by simply adding 14 days to the date. You can then check if today's date is either of those dates, e.g.

// Create a Date for 20 December, 2016 with time 00:00:00var startDate = newDate(2016,11,20);

// Create a Date for the start + 14 days with time 00:00:00var startPlus14 = newDate(startDate);
startPlus14.setDate(startPlus14.getDate() + 14);

// Get today and set the time to 00:00:00.000var today = newDate();
today.setHours(0,0,0,0);

if (+today == +startDate) {
  console.log('Today is the start date');
} elseif (+today == +startPlus14) {
  console.log('Today is 14 days after the start date');
} else {
  console.log('Today is neither the start nor 14 days after the start');
}

Post a Comment for "How To Calculate And Check For A Bi-weekly Date"