Subtract days from a date field(mm/dd/yyy) excluding weekends jquery

BetRob picture BetRob · Mar 3, 2014 · Viewed 7.5k times · Source

How can I subtract days from a date field?

Example:

Date Value is 03/10/2014 (mm/dd/yy),

I want to be able to subtract 6 weedays from it to get 03/03/2014

Can someone please help me get this.

Fiddle: to add 6 weekdays and disable all previous date values... From this fiddle whatever date is selected 6 weekdays must be subtracted from it.

i.e $("#txtFromDate").val() - 6 weekdays http://jsfiddle.net/7DHVr/8/

Thanks in advance

Answer

clancer picture clancer · Mar 3, 2014

This can be done by subtracting from a Date type variable in javascript like so

var d = new Date("08/18/2009");
d.setDate(d.getDate()-5);

This will return a string of numbers representing that date then use

new Date(new Date().setDate(new Date().getDate()-5))

If you want to reformat it as a date the detailed answer is here Subtract days from a date in JavaScript