Parse ONLY a time string with DateJS

后端 未结 2 1749
一生所求
一生所求 2021-01-02 04:34

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

2条回答
  •  耶瑟儿~
    2021-01-02 05:27

    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.

提交回复
热议问题