获取提供给moment.js的输入的函数 [英] Function to get the input provided to momentjs

查看:136
本文介绍了获取提供给moment.js的输入的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何函数可获取提供给moment()的输入

Is there any function to get the input that was provided to moment()

在下面的示例中,inputDate变为空.

In the example below, inputDate becomes null.

var date = moment("invalid date");
if(!data.isValid()){
  return { message: "Invalid date", inputDate: date }
}

我可以使用内部函数(即date._i)访问输入,但是想知道是否有任何函数可以将提供的输入返回给矩构造函数.

I can access the input using internals i.e. date._i but was wondering if there's any function that would return the input provided to moment constructor.

推荐答案

您可以使用 creationData()

You can use creationData()

在创建矩对象之后,可以使用creationData()方法访问所有输入:

After a moment object is created, all of the inputs can be accessed with creationData() method:

moment("2013-01-02", "YYYY-MM-DD", true).creationData() === {
    input: "2013-01-02",
    format: "YYYY-MM-DD",
    locale: Locale obj,
    isUTC: false,
    strict: true
}

这里有个例子:

var date = moment("invalid date", moment.ISO_8601);
console.log(date.creationData().input);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.4/moment.min.js"></script>

作为旁注:

  • I've used moment.ISO_8601 in my snippet to prevent Deprecation Warning, as shown here.
  • Something quite similar was asked (but not a duplicate) was asked here.

这篇关于获取提供给moment.js的输入的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆