1

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 present date against x and the present date against what the recurrence date will be (14) days from x as defined by the variable y.

var y = recurrence is every 14 days, thereafter from the date (x) with no end date specified (unlimited)

Ex.

function() {

    if (present date == x) { alert(true) }

    if (present date == y) { alert(true) }

}
clinton3141
  • 4,751
  • 3
  • 33
  • 46
BobbyJones
  • 1,331
  • 1
  • 26
  • 44
  • So add 14 days to a date object. – epascarello Dec 19 '16 at 21:04
  • 1
    You could get the number of days difference between two dates (http://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) and then check if that number is divisible by 14 (using modulus) – Moob Dec 19 '16 at 21:27
  • Probably a duplicate of [*Add +1 to current date*](http://stackoverflow.com/questions/9989382/add-1-to-current-date). The same method can be used to add or subtract 14 days. – RobG Dec 19 '16 at 22:26

4 Answers4

3

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.

function treatAsUTC(date) {
    var result = new Date(date);
    result.setMinutes(result.getMinutes() - result.getTimezoneOffset());
    return result;
}

function daysBetween(startDate, endDate) {
    var millisecondsPerDay = 24 * 60 * 60 * 1000;
    return Math.floor((treatAsUTC(endDate) - treatAsUTC(startDate)) / millisecondsPerDay);
}

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

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

Moob
  • 14,420
  • 1
  • 34
  • 47
  • Thanks to [@michael-liu](http://stackoverflow.com/users/1127114/michael-liu) for the `treatAsUTC` and `daysBetween` scripts taken from this answer: http://stackoverflow.com/a/11252167/1921385 – Moob Dec 20 '16 at 10:03
0

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

let y = new Date(Date.now() + (14 * 24 * 60 * 60 * 1000));
console.log(y.toUTCString()); // "Mon, 02 Jan 2017 21:03:30 GMT"
Hatchet
  • 5,320
  • 1
  • 30
  • 42
  • Not all days are exactly 24 hours long, so this will not reliably return a date 14 days from today. It's much better to add 14 to the date. – RobG Dec 19 '16 at 22:36
0

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

var present_date = new Date();
if(present_date.getTime() === x.getTime()) alert("Today is the same date as x");
else {
    var y = new Date(x.getTime());
    y.setDate(y.getDate() + 14); // add 14 days
    if(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!

nibnut
  • 3,072
  • 2
  • 15
  • 17
  • I like this approach, but y is dynamic, January 2, 2017, January 16, 2017, January 30, 2016 and so on with no end date. I guess it needs to check the present date against a loop of occurrences of some sort? – BobbyJones Dec 19 '16 at 21:14
  • So yes, you will have some kind of loop then, either a for loop if you know how far in the future you need to check, or do/while loop until you reach a certain date... What are you trying to do exactly - that might help me show you how to proceed. – nibnut Dec 19 '16 at 21:22
  • I'm trying to determine what the date (y) will be every 14 days after the date x, so if date x = December 19, 2016, y = January 2, 2016, if date x = January 2, 2017, date y = January 16, 2017, – BobbyJones Dec 19 '16 at 21:29
  • Right - but eventually you'll have to stop - or else you'll generate dates forever! ;) But here's a way to find the next 3 dates: for(var loop = 1; loop <=3; loop++) { var y = new Date(x.getTime()); y.setDate(y.getDate() + (14 * loop)); ... } – nibnut Dec 19 '16 at 21:39
0

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:00
var startDate = new Date(2016,11,20);

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

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

if (+today == +startDate) {
  console.log('Today is the start date');
} else if (+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');
}
Community
  • 1
  • 1
RobG
  • 142,382
  • 31
  • 172
  • 209