获取日期格式2016-01-01T00:00:00.000-00:00

时间:2017-01-08 17:08:48

标签: javascript node.js

我试图以mercadolibre api的格式获取日期:

2016-01-01T00:00:00.000-00:00

但是当我这样做时:

var date_from = new Date();
    date_from.setDate(date_from.getDate() - 1);
    date_from.setUTCHours(0,0,0,0);

我明白了:

2017-01-08T17:04:54.925Z

我如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

使用setUTCHours(hours, minutes, seconds, ms)和单独调用每个函数都可以正常工作。尝试运行示例下方并与完整代码进行比较。

var date_from = new Date();
    date_from.setDate(date_from.getDate() - 1);
    date_from.setUTCHours(0,0,0,0);

console.log('example 1', date_from.toISOString());

date_from = new Date();
date_from.setDate(date_from.getDate() - 1);
date_from.setUTCHours(0);
date_from.setUTCMinutes(0);
date_from.setUTCSeconds(0);
date_from.setUTCMilliseconds(0);

console.log('example 2', date_from.toISOString());