function formatDate(data) { for (var i = 0; i < data.length; i++) { var dateString = data[i].petition_at; var date = new Date(dateString); var year = date.getFullYear(); var month = ("0" + (date.getMonth() + 1)).slice(-2); var day = ("0" + date.getDate()).slice(-2); data[i].petition_at = year + "-" + month + "-" + day + " " + hours dateString = data[i].created_at; date = new Date(dateString); year = date.getFullYear(); month = ("0" + (date.getMonth() + 1)).slice(-2); day = ("0" + date.getDate()).slice(-2); data[i].petition_at = year + "-" + month + "-" + day + " " + hours data[i].created_at = date.toLocaleString(); } return data; } return data = formatDate(data)哪里有错
时间: 2024-03-26 19:38:13 浏览: 152
这段代码有几处小错误:
1. 在第一行的 `function formatDate(data)` 后面缺少一个左括号 `(`。
2. 在第三个 `var` 声明中,变量 `hours` 没有被定义,应该是 `date.getHours()`。
3. 在第六行和第九行,将 `petition_at` 改成了 `created_at`,这是无意义的,应该将第九行的 `petition_at` 改成 `created_at`。
下面是修正后的代码:
```
function formatDate(data) {
for (var i = 0; i < data.length; i++) {
var dateString = data[i].petition_at;
var date = new Date(dateString);
var year = date.getFullYear();
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var day = ("0" + date.getDate()).slice(-2);
var hours = date.getHours();
data[i].petition_at = year + "-" + month + "-" + day + " " + hours;
dateString = data[i].created_at;
date = new Date(dateString);
year = date.getFullYear();
month = ("0" + (date.getMonth() + 1)).slice(-2);
day = ("0" + date.getDate()).slice(-2);
data[i].created_at = year + "-" + month + "-" + day + " " + hours;
data[i].petition_at = date.toLocaleString();
}
return data;
}
data = formatDate(data);
```
阅读全文