获取日期格式2016

系统教程 行业动态 更新时间:2024-06-14 16:59:47
获取日期格式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:00

But 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.925Z

How 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()); 
  
 

更多推荐

本文发布于:2023-04-17 09:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/d6bdd070e7bcd92c52db299c156fd916.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   格式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!