我需要在 javascript 中创建一个方法,它将参数作为
- 日期范围(可能值为“今天”/“本周”/“本月”
- BooleanFullDate(可能值为 true/false)
现在,当参数值为Today
时,它应该返回我
10/23/2013
10/23/2013
当This Week
和BooleanFullDate为true
时,那么它应该返回我
10/20/2013
10/26/2013
当This Week
和BooleanFullDate为false
时,那么它应该返回我
10/20/2013
10/23/2013
当本月
和BooleanFullDate为true
时,那么它应该返回我
10/01/2013
10/31/2013
当This Month
和BooleanFullDate为false
时,那么它应该返回我
10/01/2013
10/23/2013
为此,我编写了代码 - JS Fiddle LINK
问题:-
现在,作为 javascript 的新手,我无法弄清楚一些事情(你也可以在 jsbin 中看到)
当我调用 GetStartAndEnd("Today");
时,为什么它返回给我 end: "9/23/2013",
而不是 end: “10/23/2013”,
(当前月份)?
最后,有人可以建议我改进代码吗?
最佳答案
When I am calling GetStartAndEnd("Today"); why is it returning me end: "9/23/2013", and not end: "10/23/2013", (Current Month) ?
嗯,
The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.
所以你必须在 getMonth()
中添加+1
意思是:
var lmonth = lastDay.getMonth() + 1;
关于javascript - JavaScript 中的日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19533894/