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

javascript - Moment.js: Check if string is in correct format - Stack Overflow

programmeradmin3浏览0评论

I was checking if the moment string is correct using a regex with a specific format string:

const isCorrect = isCorrectFormat('12/06/2016 20:20:20:444')

and the isCorrectFormat function:

const isCorrectFormat = (dateString) => {
    const regex = /[0-3][0-9][/][0-9]{2}[/][0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]*/
    return regex.test(dateString)
}

And of course the result in this case will be false because is not using the same format.

The problem that I want to solve now, is send the format also as parameter, but instead of using a regex, I want to use directly the moment format speficifation.

const isCorrect = isCorrectFormat(
    '12/06/2016 20:20:20:444',
    'MM/DD/YYYY HH:mm:ss.SSS'
)

But I don't have idea how to implement it... I found in the documentation of moment and I don't see any method to test it. Any idea?

Thank you so much!!

I was checking if the moment string is correct using a regex with a specific format string:

const isCorrect = isCorrectFormat('12/06/2016 20:20:20:444')

and the isCorrectFormat function:

const isCorrectFormat = (dateString) => {
    const regex = /[0-3][0-9][/][0-9]{2}[/][0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]*/
    return regex.test(dateString)
}

And of course the result in this case will be false because is not using the same format.

The problem that I want to solve now, is send the format also as parameter, but instead of using a regex, I want to use directly the moment format speficifation.

const isCorrect = isCorrectFormat(
    '12/06/2016 20:20:20:444',
    'MM/DD/YYYY HH:mm:ss.SSS'
)

But I don't have idea how to implement it... I found in the documentation of moment and I don't see any method to test it. Any idea?

Thank you so much!!

Share Improve this question asked Jul 26, 2017 at 11:14 Aral RocaAral Roca 5,8998 gold badges52 silver badges82 bronze badges 2
  • 1 Moment will try and parse whatever you pass it. If you know the format then use momentjs.com/docs/#/parsing/string-format – Josef Van Zyl Commented Jul 26, 2017 at 11:16
  • moment("not a real date").isValid(); // false – Josef Van Zyl Commented Jul 26, 2017 at 11:18
Add a comment  | 

2 Answers 2

Reset to default 20

You can use moment strict parsing and isValid method.

As stated in moment(String, String) docs:

Moment's parser is very forgiving, and this can lead to undesired/unexpected behavior.

As of version 2.3.0, you may specify a boolean for the last argument to make Moment use strict parsing. Strict parsing requires that the format and input match exactly, including delimeters.

Herea working sample:

const isCorrectFormat = (dateString, format) => {
    return moment(dateString, format, true).isValid()
}

const isCorrect = isCorrectFormat(
    '12/06/2016 20:20:20:444',
    'MM/DD/YYYY HH:mm:ss.SSS'
)

console.log(isCorrect);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

You can do this using moment:

var date = moment('12/06/2016 20:20:20:444', 'MM/DD/YYYY HH:mm:ss.SSS');  

And then use moments validation:

date.isValid(); // returns true or false

http://momentjs.com/docs/#/parsing/string-format/

发布评论

评论列表(0)

  1. 暂无评论