最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Function to get the input provided to momentjs - Stack Overflow

programmeradmin1浏览0评论

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

In the example below, inputDate bees null.

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

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.

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

In the example below, inputDate bees null.

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

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.

Share Improve this question asked Dec 20, 2017 at 13:23 SamiSami 4,0061 gold badge32 silver badges42 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

You can use 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
}

Here a live example:

var date = moment("invalid date", moment.ISO_8601);
console.log(date.creationData().input);
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.19.4/moment.min.js"></script>

As a side note:

  • 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.
发布评论

评论列表(0)

  1. 暂无评论