Reputation: 1874
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 = startDate.day("Friday");
alert(startDate);
alert(nf);
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.
Upvotes: 2
Views: 1027
Reputation: 3423
Here is how I count the number of weeks inbetween two days...
//Used for counting weeks since
var baseDate = new Date("01/01/2014");
//date is in format 01/01/2014
var startTimeTokens1 = data.filterStartTime.split('/');
//Calculate the startWeek number
var sDate = new Date(startTimeTokens1[0] + "/" + startTimeTokens1[1] + "/" + startTimeTokens1[2]); //sDate looks like 01/07/2014
var stimeDiff = Math.abs(sDate.getTime() - baseDate.getTime());
var sdiffDays = Math.ceil(stimeDiff / (1000 * 3600 * 24));
startWeek = (Math.ceil( sdiffDays/7 ) + 1);
Upvotes: 0
Reputation: 8851
Try:
var startDate = moment("10/1/2014");
var nf = moment("10/1/2014").day("Friday");
alert(startDate);
alert(nf);
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
Upvotes: 6