Skip to main content

The question is simple, but I suspect the answer is not quite so.

I know how to use the getDate() and setDate() functions, but they give me the day of the month. Doesn’t work when I want to add 6 days to January 31, 2015. Oddly enough the answer I get back that is March 9, 2015. Watch the video to see what I mean.

I basically want to do what I thought would be easy in javascript. Like this example from datajs.

4a60ac6cac044b8195840e27bbbfcd1e5198a70b.png


Take a look here:  http://www.javascriptcookbook.com/article/Perform-date-manipulations-based-on-adding-or-subtracting-…

To add 3 days you would do something like this:

var date = new Date();
date.setDate(date.getDate() + 3);




That doesn’t work as it sets the day of the month. So if it were Feb. 28 today but then I set the startDate field to Jan. 1, endDate would end being Feb. 4.

I should’ve included my code.


var params = argumentse0], $ = skuid.$, JRModel = skuid.$M('REP_JobReport'), JRModelRow = JRModel.getFirstRow(), startDay = new Date(), endDay = new Date(); //The start date is set on the skuid page and this snippet is run when the report type, RECON__Dayof__c or RECON__WeekOf__c is updated if (JRModel.getFieldValue(JRModelRow,'RECON__Report_Type__c')=='Daily') { startDay = skuid.time.parseSFDate(JRModel.getFieldValue(JRModelRow,'RECON__Dayof__c')); endDay = startDay; } else { startDay = skuid.time.parseSFDate(JRModel.getFieldValue(JRModelRow,'RECON__WeekOf__c')); endDay.setDate(startDay.getDate() + 6); } var fields = { RECON__Display_Start_Date__c: skuid.time.getSFDate(startDay), RECON__Display_End_Date__c: skuid.time.getSFDate(endDay) }; JRModel.updateRow(JRModelRow,fields);<br>

Hi Pat,

Lets step through the important parts of your code…

startDay = new Date();   // startDay is now today’s date say 2015-02-20
endDay = new Date();    // endDay is now today’s date say 2015-02-20


startDay = skuid.time.parseDate(…);   // startDay is now the day you picked 2015-01-31
endDay.setDate(startDay.getDate() + 6);  // endDay is now Today’s year, and today’s month, but with 37 days.  So 2015-02-37. Javascript interprets that as march 9th!

Javascript dates are pretty confusing and this stuff has gotten me so many times.  To fix this, clone the end date first.

startDay = skuid.time.parseDate(…);
endDay = new Date(startDay.getTime());   // This makes endDay a clone of startDay
endDay.setDate(startDay.getDate() + 6);


I get it now … I think. I first have to update the endDay to be equal to the current startDay date before trying to add days to it. The one thing I haven’t tried, or new about, was “cloning” the startDate Date. So, adding days to a date object works just fine. ie. adding 6 days to Jan. 31 gets Feb. 6.


Yup, you got it.  To make your code less confusing, I’d probably make that last line use endDay only.  Like this…

endDay.setDate(endDay.getDate() + 6);

That way it’s pretty obvious that you’re just adding 6 days to endDay.


Done and done. Thank you!


Reply