简体   繁体   English

MomentJS从日期对象中删除时间(javascript)

[英]MomentJS remove Time from Date Object (javascript)

I am using Moment.js to create a new date object and pushing to an array using the following code. 我正在使用Moment.js创建新的日期对象,并使用以下代码将其推送到数组。

array.push(moment(object[column]).toDate());

The value of 'object[column]' is 2015-01-02 before creating the date object. 创建日期对象之前,“ object [column]”的值是2015-01-02。

This array is used to generate a Google Chart using their API 该数组用于使用其API生成Google图表

The date becomes the following. 日期变为以下。

Fri Jan 02 2015 00:00:00 GMT-0500 (Eastern Standard Time)

I would like to parse it, to use it in the X Axis. 我想解析它,以在X轴中使用它。 I have tried the following but the console starts yelling. 我尝试了以下操作,但控制台开始大喊大叫。

array.push(moment(object[column]).toDate().format('MM/DD/YYYY'));
Uncaught TypeError: moment(...).toDate(...).format is not a function

I have tried this and the dates are off by months! 我已经尝试过了,日期已经过了几个月!

array.push(moment(object[column], 'MM/DD/YYYY').toDate());
Messed up data -"Wed Aug 15 1 00:00:00 GMT-0400 (Eastern Daylight Time),9,8,7,6,5,4,3,2,1

The outcome I am looking for is 1/1/15 - Am I missing something? 我要寻找的结果是1/1/15-我缺少什么吗? Thank you. 谢谢。

Consider: 考虑:

moment("2015-01-02").format("MM/DD/YYYY")  === "01/02/2015"

Putting it together with the other things you mentioned, you're probably looking for 将其与您提到的其他内容放在一起,您可能正在寻找

array.push(moment(object[column]).format("MM/DD/YYYY"));

That pushes a formatted string into your array. 这会将格式化的字符串推入数组。 If instead you want a date object, then you would just push the date object: 相反,如果您想要一个日期对象,则只需按下日期对象:

array.push(moment(object[column]).toDate());

If it's just that it's not displaying in the chart the way you like, then use a DateFormat formatter, as described in the Google Charts documentation . 如果只是它没有按照您希望的方式显示在图表中,请使用DateFormat格式化程序,如Google Charts文档中所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM