Brainchild Brainchild - 15 days ago 4x
Javascript Question

find next friday from a fixed date

I would like to find out the next coming date from a given date. I am using moment js and below is my code.

var startDate = moment("10/1/2014");
var nf ="Friday");


the code is returning next friday, but the above piece of code is changing the original object (startDate) also to next friday. the returned object is a complex object not sure which property to use.

Updating the question with complete code.

var startDate = moment("10/1/2014");
var endDate = moment( '10/31/2014');

var diff = endDate.diff(startDate,'days');

var noOfWeeks = diff / 7;

var nextStartday = startDate;
var nextFriday = moment( startDate).day("Friday");

for (var j = 0; j < noOfWeeks ; j++) {
newRow.PlStartDateandTime = nextStartday;
newRow.PlEndDateandTime = nextFriday;

nextStartday = moment( nextFriday).day("Monday");
nextFriday = moment( nextStartday).day("Friday");

My expectation is to find the weekdays in between the given time frame.



var startDate = moment("10/1/2014");
var nf = moment("10/1/2014").day("Friday");


You can also do (as you asked) var nf = moment(startDate).day("Friday");

You are setting the day of startDate which is changing it, you need to make a separate object if you want to be able to have two different values.

If your diff gives the wrong answer, it's because you set it before changing the dates. It won't dynamically update for you, you need to re assign its value