【问题标题】:Extracting only date from moment object仅从时刻对象中提取日期
【发布时间】:2018-10-12 02:47:19
【问题描述】:

我正在使用 ion2 日历来创建多日期选择器。这里我使用的是 onSelect 事件发射器。

onSelect($event) {

 console.log("onSelect event called");
 console.log($event);  

};

当我打印 '$event' 时,控制台输出如下所示:

如何仅从“DD-MMMM-YYYY”格式的时刻对象中提取日期?

【问题讨论】:

    标签: ionic-framework momentjs


    【解决方案1】:

    下面的一个衬垫会给你想要的输出:

    let formattedDate = moment($event[0].Moment._d).format('DD-MMMM-YYYY');
    

    所以你的函数会变成

    onSelect($event) {
        let formattedDate = moment($event[0].Moment._d).format('DD-MMMM-YYYY');
        console.log("onSelect event called");
        console.log($event);  
    };
    

    注意:由于我无法调试您的代码,因此请先检查 $event[0].Moment._d 是否在您的问题。

    【讨论】:

    • 您好 VicJordan,感谢您的回复。 DD-MMMM-YYYY 格式用于显示月份的全名。例如:2014 年 8 月 28 日
    • @ChaitraR 刚刚更新了我的答案。它将工作并打印完整的月份名称。看看吧。
    • 我收到此错误:TypeError: Cannot read property '_d' of undefined
    • @ChaitraR 更新了答案。请与$event[0].Moment._d 联系。它应该可以工作。
    • 现在我收到了这个错误:TypeError: Cannot read property 'Moment' of undefined
    【解决方案2】:

    在你的情况下,只需使用moment($event[0]).format('DD-MMMM-YYYY')

    console.log(moment().format('DD-MMMM-YYYY'))
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>

    【讨论】:

    • 这是折旧的
    • @Hunt,什么被弃用了?
    猜你喜欢
    • 1970-01-01
    • 2017-04-05
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    相关资源
    最近更新 更多