Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 408
Tauren Avatar asked Oct 25 '10 07:10

Tauren


People also ask

How to parse DateTime in JavaScript?

parse() The Date. parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31). Only the ISO 8601 format ( YYYY-MM-DDTHH:mm:ss.

How to get time stamp from Date?

Getting the Current Time Stamp If you instead want to get the current time stamp, you can create a new Date object and use the getTime() method. const currentDate = new Date(); const timestamp = currentDate. getTime(); In JavaScript, a time stamp is the number of milliseconds that have passed since January 1, 1970.

How to get current time in millis in js?

var currentTime = +new Date(); This gives you the current time in milliseconds.

How to Convert string into Date JavaScript?

Use the Date() constructor to convert a string to a Date object, e.g. const date = new Date('2022-09-24') . The Date() constructor takes a valid date string as a parameter and returns a Date object. Copied! We used the Date() constructor to convert a string to a Date object.


1 Answers

Shortly after asking my question, I discovered that Date.parseExact() can take an array of format strings. Somehow I'm missed that. I managed to get something working with the following code:

function validateTime(input) {
    return Date.parseExact(input, [
            "H:m",
            "h:mt",
            "h:m t",
            "ht","h t"]) != null ||
        Date.parseExact(input, [
            "h:mtt",
            "h:m tt",
            "htt","h tt"]) != null;
};

Note that some formats don't seem to be able to be included together at the same time, which is why I split them into two separate parseExact() calls. In this case, I couldn't include any string that contained a single t in it with format strings that contained a double tt in it.

like image 90
Tauren Avatar answered Sep 23 '22 06:09

Tauren