我必須計算前一天從數據庫中選擇數據進行報告。它一直工作到2016年6月30日,但在7月1日失敗。 2016-06-30(一號)取得2016-05-31的日期。我的功能是使用節點調度名爲每天一次,看起來像這樣:在node.js中獲取以前的日期
cron.scheduleJob({hour: 0, minute: 5}, function() {
nowDate = new Date();
fromDate.setDate(nowDate.getDate() - 1);
toDate.setDate(nowDate.getDate() - 1);
logger.info("Started daily reporting ('"+fromDate.getFullYear() + "-" + (fromDate.getMonth() + 1) + "-" + fromDate.getDate() + " 00:00:00'"+" - '"+toDate.getFullYear() + "-" + (toDate.getMonth() + 1) + "-" + toDate.getDate() + " 23:59:59')");
proc.run(fromDate, toDate, function (error) {
if (error) {
logger.error(error);
} else {
logger.info("Finished daily reporting");
}
});
});
因此,在7月1日的產量爲:
Started daily reporting ('2016-5-31 00:00:00' - '2016-5-31 23:59:59'
任何想法,爲什麼我前一天的計算失敗?
作爲一個非常普通照會,日期和時區處理是一個痛苦的臀部,我通常會建議你使用像moment.js這樣的庫來避免處理它 –