获取日期格式2016-01-01T00:00:00.000-00:00(Get date in format 2016-01-01T00:00:00.000-00:00)
我正在尝试以这种格式为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我怎么解决这个问题?
I'm trying to get a date in this format for mercadolibre api:
2016-01-01T00:00:00.000-00:00But when I do:
var date_from = new Date(); date_from.setDate(date_from.getDate() - 1); date_from.setUTCHours(0,0,0,0);I get:
2017-01-08T17:04:54.925ZHow I fix this?
最满意答案
两者都使用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());Both using setUTCHours(hours, minutes, seconds, ms) and calling each function individually work. Try running the example bellow and compare to your full code.
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());
更多推荐
发布评论