check format of date with moment.js

前端 未结 4 1506
-上瘾入骨i
-上瘾入骨i 2021-02-12 17:46

I am taking input from calendar in my screen which is of this type

DD-MMM-YYYY HH:mm a

but user can provide date from keyboard. Now I have to

相关标签:
4条回答
  • 2021-02-12 18:35

    If you don't have a particular date format then you can also use Array of formats,

    moment(checked_date, [DATE_FORMAT1, DATE_FORMAT2]).format(DATE_FORMAT)
     === checked_date
    
    0 讨论(0)
  • 2021-02-12 18:39

    if it's just for checking the below is a better alternative

    moment(scope.modelValue, 'DD-MMM-YYYY HH:mm a', true).isValid()
    
    0 讨论(0)
  • 2021-02-12 18:39

    This is much better:

    if (!moment(checked_date, DATE_FORMAT).isValid()) throw Error(Argument passed is invalid: checked_date. Date format must be ${DATE_FORMAT});

    0 讨论(0)
  • 2021-02-12 18:41

    For checking date format you could use:

    moment(checked_date, DATE_FORMAT).format(DATE_FORMAT) === checked_date
    
    0 讨论(0)
提交回复
热议问题