我正在使用 Moment.js 创建一个新的日期对象,并使用以下代码推送到数组。
array.push(moment(object[column]).toDate());
创建日期对象之前,“object[column]”的值为 2015-01-02。
该数组用于使用其 API 生成 Google 图表
日期如下。
Fri Jan 02 2015 00:00:00 GMT-0500 (Eastern Standard Time)
我想解析它,在 X 轴上使用它。我已尝试以下操作,但控制台开始大喊大叫。
array.push(moment(object[column]).toDate().format('MM/DD/YYYY'));
Uncaught TypeError: moment(...).toDate(...).format is not a function
我已经尝试过了,日期晚了几个月!
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
我正在寻找的结果是 1/1/15 - 我错过了什么吗?谢谢。
最佳答案
考虑:
moment("2015-01-02").format("MM/DD/YYYY") === "01/02/2015"
将其与您提到的其他内容放在一起,您可能正在寻找
array.push(moment(object[column]).format("MM/DD/YYYY"));
这会将格式化字符串插入数组中。如果您想要一个日期对象,那么您只需推送日期对象即可:
array.push(moment(object[column]).toDate());
如果只是没有按照您喜欢的方式在图表中显示,请使用 DateFormat
格式化程序,如 the Google Charts documentation 中所述。 .
关于javascript - MomentJS 从日期对象中删除时间 (javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31321092/