Ms- 发表于4年前

• 发表于 4年前
• 阅读 36
• 收藏 0
• 评论 0
 `//一天的毫秒数`
 `02` `var` `oneDay = 1000 * 60 * 60 * 24;`
 `03`
 `04` `//from:起始`
 `05` `//to:截止`
 `06` `function` `calcWeekend(from, to){`
 `07` `    ``//两个日期相距的天数`
 `08` `    ``var` `interval = Math.floor(to.getTime() / oneDay) - Math.floor(from.getTime() / oneDay);`
 `09`
 `10` `    ``//计算星期几`
 `11` `    ``var` `x = from.getDay(); ``//0-6`
 `12`
 `13` `    ``//计算多少个星期`
 `14` `    ``var` `weeks = Math.floor(interval / 7);`
 `15`
 `16` `    ``//计算零头`
 `17` `    ``var` `rest = interval - weeks*7;`
 `18`
 `19` `    ``//根据当天是星期几，加上零头，计算截止日是星期几`
 `20` `    ``var` `y = x + rest;`
 `21`
 `22` `    ``//每周算2天周末，计算一共多少个周末`
 `23` `    ``var` `weekend = weeks * 2;`
 `24` `    ``//处理临界点，比如起始日是周日`
 `25` `    ``if``(x == 6){`
 `26` `        ``weekend -= 1;`
 `27` `    ``}`
 `28` `    ``if``(y == 6){`
 `29` `        ``weekend += 1;`
 `30` `    ``}`
 `31` `    ``else` `if``(y > 5){`
 `32` `        ``weekend += 2;`
 `33` `    ``}`
 `34`
 `35` `    ``//weekend 的值就是周末的天数`
 `36`
 `37` `    ``//weekday 的值就是工作日的天数`
 `38` `    ``weekday =interval - weekend;`
 `39` `}`

×