Parse ONLY a time string with DateJS

后端 未结 2 1705
一生所求
一生所求 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:11

    The additive approach seems cumbersome. Takes away the beauty of DateJS in my opinion. I needed the same solution and decided to just sneakily append the date in front of my input string before parsing with DateJS:

    var parsed = Date.parse(Date.today().toString('M/d/yyyy') + ' ' + this.value);
    
    if (parsed) {
      alert(parsed.toString('h:mm tt'));
    }
    

    Now DateJS will not be sniffing around for any of its date-part parsing patterns, as you have already subbed it in.

    Hope this helps someone!

提交回复
热议问题