当我做新的Date() . getMonth()1它给出111代替os 12月即12.我知道它从0索引,但我无法弄清楚如何添加1 . 任何帮助都会有所帮助!
client.on('friendMessage', (friend, message) => {
fs.writeFile("./ChatLogs/UserLogs/" + friend.getSteamID64() + "-log-" + new Date().getDate() + "-" + new Date().getMonth()+ 1 + "-" + new Date().getFullYear() + ".json", JSON.stringify({ logs: userLogs[friend.getSteamID64()] }), (ERR) => {
if (ERR) {
console.log("## An error occurred while writing UserLogs file: " + ERR);
}
});
chatLogs += friend.getSteamID64() + " : " + message + "\n";
fs.writeFile("./ChatLogs/FullLogs/log-" + new Date().getDate() + "-" + new Date().getMonth()+ 1 + "-" + new Date().getFullYear() + ".txt", chatLogs, (ERR) => {
if (ERR) {
console.log("## An error occurred while writing FullLogs file: " + ERR);
}
});
}):;
1 回答
出现这种情况是因为
"-" + new Date().getMonth()
是字符串的串联(月份转换为字符串) . 因此,当您添加1时,它再次将其转换为字符串,并进行连接 . 只需加上括号,使其对两个数字进行操作 ."-" + (new Date().getMonth()+ 1) + "-"