简体   繁体   English

使用MomentJS格式化日期

[英]Use MomentJS to format date

I cannot get pikaday to work and return the date in the format I'm asking moment.js to produce. 我无法使用pikaday并以我要求moment.js生成的格式返回日期。

var picker = new Pikaday(
    {
        field: document.getElementById('startdate'),
        firstDay: 1,
        minDate: new Date('2000-01-01'),
        maxDate: new Date('2020-12-31'),
        yearRange: [2000,2020],
        onSelect: function(date) {

          conosle.log(moment(picker.toString(), "MM-DD-YYYY"));

        }

    });

http://jsbin.com/besatocafelo/1/edit http://jsbin.com/besatocafelo/1/edit

Also, how can I get the properly formatted date into the input . 另外,如何将正确格式化的日期inputinput

The right way to format a date with moment.js is: 使用moment.js格式化日期的正确方法是:

yourMomentDate.format('DD/MM/YYYY') //or whatever format you want

So you should fix it to; 因此,您应该将其修复为;

console.log(moment(picker.toString(), "MM-DD-YYYY").format("DD/MM/YYYY"));

Since Picker gives you a date object you can do; 由于Picker为您提供了一个日期对象,因此您可以执行;

console.log(moment(picker).format("DD/MM/YYYY"));

you may pass a date object or date string to moment and then call format on it. 您可以将日期对象或日期字符串传递给一下,然后在其上调用格式。 I think what you're trying to do is this: 我认为您正在尝试执行以下操作:

moment(date).format("MM/DD/YY")

or 要么

moment(picker.toString()).format("MM/DD/YY")

Note: Your jsbin is missing a reference to momentJS, and has a typo on console.log 注意:您的jsbin缺少对momentJS的引用,并且在console.log上有错字

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

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