Javascript parsing Times without Date

后端 未结 5 2020
轻奢々
轻奢々 2020-12-08 18:22

I need to parse and manipulate Times without Dates in my code. For example i might get the string \"15:00\" from a timepicker. I want to turn this into a Time object of some

5条回答
  •  伪装坚强ぢ
    2020-12-08 19:13

    I had to do this recently for a project but didnt really need to include moment.js, the method I used was to manually parse the time like this:

    function parseTime(time) {    
        let timeInt = parseInt(time);
        let minutes = time.substring(3,5);
    
        // you could then add or subtract time here as needed
    
        if(time > '12:00') {
             return `${timeInt - 12}:${minutes} PM`;
        } else {
             return `${timeInt}:${minutes} AM`;
        }
    }
    

    Use this as an alternative starter if you don't want to use moment. Note this example uses es6 syntax.

提交回复
热议问题