Parse ONLY a time string with DateJS

I'm using the excellent (but large) DateJS library to handle dates and times in my webapp. I just came across something that I'm not sure how to handle.

I want my users to be able to enter Time strings only, without a date, but they should be able to enter it in any manner they please. For instance:

  • 5:00 pm
  • 17:00
  • 5:00pm
  • 5:00p
  • 5p
  • etc.

Using Date.parse(value) converts these strings into a full date, which is exactly what I want. However, it also allows the user to enter any other part of a date string, such as:

  • sat 5pm
  • 1/1/2010 5pm
  • etc.

I'm trying to use DateJS to validate an input field for a time value. Something like:

function validateTime(value) {
    return Date.parse(value) !== null;
}

Is there a way to use DateJS features to solve this? There are other SO questions that provide solutions, but if DateJS has a way to do this, I don't really want to add more custom code to my app to do this.

10
задан Community 23 May 2017 в 12:15
поделиться