0

I want to write a jQuery code, which calculates the date of upcoming week day.

It is for arranging the meetings. manager chooses the meeting days, with weekly period. for example every Saturday. Employee looks into the meeting panel, and notifies when is the next meeting. it has to show the date, according to Today's date. and when it expired, again calculates it for the upcoming week.

the problem is that, I can't find the way to calculate the chosen day's date, correctly. I wrote this :

var d = $("#day").html();
var x = 0;

if (d === "Saturday") { 
    var x = 0;
};
if (d === "Sunday") { 
    var x = 1;
};
if (d === "Monday") { 
    var x = 2;
};

if (d === "Tuesday") { 
    var x = 3;
};
if (d === "Wednesday") { 
    var x = 4;
};
if (d === "Thursday") { 
    var x = 5;
};
if (d === "Friday") { 
    var x = 6;
};
var b = new Date();
var n = b.getDay();
var c = n - x;

now c is the difference between today and the upcoming meeting date. for finding the date of meeting, I am going to use moment.js library like this

moment().add("day", x).toString();

but it is not returning the date for me. Do you have a better algorithm ?

ehsan badakhshan
  • 135
  • 2
  • 11

1 Answers1

0

You might want to try:

var d = $("#day").html();
var x = 0;

if (d === "Saturday") { 
    var x = 6;
};
if (d === "Sunday") { 
    var x = 0;
};
if (d === "Monday") { 
    var x = 1;
};

if (d === "Tuesday") { 
    var x = 2;
};
if (d === "Wednesday") { 
    var x = 3;
};
if (d === "Thursday") { 
    var x = 4;
};
if (d === "Friday") { 
    var x = 5;
};
var b = new Date();
var n = b.getDay();
var c = n - x;
var newDate = new Date();
newDate.setDate(b.getDate() + c);

//newDate should now have your new date for the next meeting

Code for incrementing date taken from here

Community
  • 1
  • 1
brso05
  • 13,142
  • 2
  • 21
  • 40