Php

JAVASCRIPT – MomentJs date and time validation

A good way to validate date and time in javascrit is with momentJs:

<script src="https://momentjs.com/downloads/moment.js"></script>

Ex:

const allPossibleFormats = [
  'D MMMM YYYY',
  'D MMMM YYYY HH:mm',
  'DD-MM-YY',
  'DD-MM-YYYY',
  'DD.MM.YYYY',
  'DD.MM.YYYY HH:mm',
  'DD/MM/YY',
  'DD/MM/YYYY',
  'DD/MM/YYYY HH:mm:ss',
  'HH:mm:ss',
  'M/D/YYYY',
  'D/M/YYYY',
  'MM-DD-YY',
  'MM-DD-YYYY',
  'MM-DD-YYYY HH:mm:ss',
  'MM/DD/YY',
  'MM/DD/YYYY',
  'MM/DD/YYYY HH:mm:ss',
  'MMM D YYYY',
  'MMM D YYYY LT',
  'MMMM Do YYYY',
  'MMMM Do YYYY LT',
  'YYYY-DD-MM HH:mm:ss',
  'YYYY-MM',
  'YYYY-MM-DD',
  'YYYY-MM-DD HH:mm',
  'YYYY-MM-DD HH:mm:ss',
  'YYYY-MM-DD LT',
  'YYYY-MM-DD h:mm:ss A',
  'YYYY-MM-DDTHH:mm:ssZ',
  'ddd, MMM D YYYY LT',
  'dddd D MMMM YYYY HH:mm',
  'dddd, MMMM Do YYYY LT'
];

moment('Chicago Illinois 46702', allPossibleFormats, true).isValid(); // false
moment('18/01/1944', allPossibleFormats, true).isValid(); // true
moment('22-10-2020', allPossibleFormats, true).isValid(); // true
moment('1944-01-18 12:00:00', allPossibleFormats, true).isValid(); // true
moment('2001-01-01 00:00:00', allPossibleFormats, true).isValid(); // true
moment('2001-01', allPossibleFormats, true).isValid(); // true

Source: https://stackoverflow.com/a/58621119/3476347

Similar Posts