我怎样才能得到之前的year开始date/month和结束date/month
下面是代码,我已经尝试过,但它不起作用......
var lastyear = new Date(new Date().getFullYear(), 0, 1);
lastyear.setFullYear(lastyear.getFullYear() - 1);
var start = (new Date(lastyear, 0, 1)).getTime(),
end = (new Date(lastyear, 11, 31)).getTime();
Run Code Online (Sandbox Code Playgroud)
从技术上讲我01/01/2015想12/31/2015。我在这里犯了什么错误?
您遇到问题是因为您错误地使用了 Date() 构造函数。根据http://www.w3schools.com/js/js_dates.asp, Date 构造函数接受以下输入:
new Date(year, month, day, hours, minutes, seconds, milliseconds)
Run Code Online (Sandbox Code Playgroud)
在原始代码中,您将 Date 对象传递到“year”参数中。我变了:
new Date(lastyear, 0, 1)
Run Code Online (Sandbox Code Playgroud)
到
new Date(lastyear.getFullYear(), 0, 1)
Run Code Online (Sandbox Code Playgroud)
这解决了问题。
var lastyear = new Date(new Date().getFullYear() - 1, 0, 1);
var start = (new Date(lastyear.getFullYear(), 0, 1)).getTime(),
end = (new Date(lastyear.getFullYear(), 11, 31)).getTime();
Run Code Online (Sandbox Code Playgroud)
这是您正在寻找的解决方案吗?
| 归档时间: |
|
| 查看次数: |
2357 次 |
| 最近记录: |