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

37 thoughts on “JAVASCRIPT – MomentJs date and time validation
  1. It’s really a great and helpful piece of info.
    I’m glad that you just shared this helpful info with us.

    Please stay us informed like this. Thanks for sharing.

  2. With havin so much content and articles do you ever run into any
    issues of plagorism or copyright violation?
    My blog has a lot of unique content I’ve either authored myself or outsourced
    but it looks like a lot of it is popping it up all over
    the web without my agreement. Do you know any techniques to help protect against content from being ripped off?
    I’d truly appreciate it.

  3. Nice blog here! Also your website loads up fast!
    What web host are you using? Can I get your affiliate link
    to your host? I wish my website loaded up as quickly as yours lol

  4. After looking over a handful of the blog posts on your blog, I really appreciate your way of
    blogging. I saved it to my bookmark website list
    and will be checking back in the near future. Please visit my web site as well
    and tell me how you feel.

  5. Quality posts is the important to interest the visitors to pay a quick visit the
    site, that’s what this web page is providing.

  6. What’s up, yeah this paragraph is genuinely fastidious and I
    have learned lot of things from it regarding blogging. thanks.

  7. It’s perfect time to make a few plans for the longer term
    and it’s time to be happy. I’ve read this publish and if I could I
    wish to suggest you some interesting issues or suggestions.

    Perhaps you can write next articles relating to this article.
    I wish to learn more things approximately it!

  8. I do accept as true with all the ideas you’ve introduced on your
    post. They are very convincing and can definitely work.

    Nonetheless, the posts are very short for novices. May you
    please extend them a bit from next time? Thank you for the post.

  9. My spouse and I stumbled over here by a different website
    and thought I might check things out. I like what
    I see so i am just following you. Look forward to checking
    out your web page again.

  10. It’s going to be finish of mine day, however before ending I am
    reading this great post to improve my know-how.

  11. I am really impressed with your writing skills as well as with
    the layout on your blog. Is this a paid theme or did you modify it yourself?

    Anyway keep up the excellent quality writing, it is rare to see a nice blog like this one nowadays.

  12. Really when someone doesn’t understand after that its
    up to other users that they will assist, so here it happens.

  13. Attractive section of content. I just stumbled upon your site and
    in accession capital to assert that I acquire actually enjoyed account your blog
    posts. Anyway I’ll be subscribing to your feeds and even I achievement you access
    consistently rapidly.

  14. Just wish to say your article is as amazing. The clarity in your
    post is simply nice and i could assume you are an expert
    on this subject. Fine with your permission let me to grab your feed to keep up to date with forthcoming post.

    Thanks a million and please keep up the gratifying work.

  15. An intriguing discussion is definitely worth comment.

    I do think that you need to write more on this topic, it might not be a taboo subject
    but generally people do not talk about such subjects.
    To the next! Kind regards!!

  16. Howdy this is somewhat of off topic but I was wanting to know if blogs use WYSIWYG editors or if
    you have to manually code with HTML. I’m starting a blog soon but have
    no coding skills so I wanted to get guidance from someone
    with experience. Any help would be enormously appreciated!

  17. Admiring the time and energy you put into your site and detailed information you present.

    It’s good to come across a blog every once in a while that isn’t the same out of date rehashed material.
    Great read! I’ve bookmarked your site and I’m adding your RSS feeds to my Google
    account.

  18. It’s really a cool and useful piece of info.
    I am happy that you just shared this useful information with us.
    Please keep us informed like this. Thank you for sharing.

  19. My coder is trying to convince me to move to .net from
    PHP. I have always disliked the idea because of the costs.

    But he’s tryiong none the less. I’ve been using Movable-type on a
    number of websites for about a year and am worried about switching to
    another platform. I have heard very good things about blogengine.net.
    Is there a way I can import all my wordpress posts into it?

    Any kind of help would be really appreciated!

  20. Hello There. I found your blog using msn. This is a very well
    written article. I will make sure to bookmark it and return to read more
    of your useful information. Thanks for the post. I’ll certainly comeback.

  21. Hey there great blog! Does running a blog similar to this take
    a massive amount work? I’ve virtually no knowledge of computer programming but I
    had been hoping to start my own blog in the near future. Anyhow,
    should you have any ideas or tips for new blog owners
    please share. I know this is off topic however I simply wanted to ask.
    Thank you!

  22. I don’t know whether it’s just me or if perhaps everybody else experiencing problems with your site.

    It appears as though some of the text within your content
    are running off the screen. Can someone else please provide feedback
    and let me know if this is happening to them too?
    This could be a problem with my web browser because I’ve
    had this happen before. Kudos

Leave a Reply

Your email address will not be published.