Javascript parsing Times without Date

后端 未结 5 2009
轻奢々
轻奢々 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 18:51

    Okay, so I know I'm way late to the party. Like 6 years late but this was something I needed to figure out and have it formatted HH:mm:ss (24 hours).

    moment().format(moment.HTML5_FMT.TIME_SECONDS); // 17:44:56
    

    You can also pass in a parameter like, 2019-11-08T17:44:56.144.

    moment('2019-11-08T17:44:56.144').format(moment.HTML5_FMT.TIME_SECONDS); // 17:44:56
    

    https://momentjs.com/docs/#/parsing/special-formats/

    0 讨论(0)
  • 2020-12-08 19:04

    Here's a moment.js solution for 12 or 24 hour times:

    moment('7:00 am', ['h:m a', 'H:m']); // Wed Dec 30 2015 07:00:00 GMT-0600 (CST)
    moment('17:00', ['h:m a', 'H:m']);   // Wed Dec 30 2015 17:00:00 GMT-0600 (CST)
    moment('17:00 am', ['h:m a', 'H:m']);// Wed Dec 30 2015 17:00:00 GMT-0600 (CST)
    moment('17:00 pm', ['h:m a', 'H:m']);// Wed Dec 30 2015 17:00:00 GMT-0600 (CST)
    

    http://momentjs.com/docs/#/parsing/string-formats/

    0 讨论(0)
  • 2020-12-08 19:07

    I ended up using the following since I was already using moment in my app:

    var str = '15:16:33';
    var d = new moment(str, 'HH:mm:ss');
    

    See Moment String+Format docs for other format strings you can use.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-08 19:14

    Unfortunately, there's not a great solution. JavaScript only has a Date object, which is probably misnamed since it is really a date+time.

    One thing you might want to think about deeper - you say you want to work with only time, but do you mean a time-of-day or do you mean a duration of time? These are two related, but slightly different concepts.

    For example, you said you might want an operation like "15:00 + 1 hour". Well that would clearly be 16:00 either way. But what about "15:00 + 10 hours"? It would be 25:00 if you are talking about a duration, but it might be 01:00 if you are talking about time-of-day.

    Actually, it might not be 01:00, since not all days have 24 hours in them. Some days have 23, 23.5, 24.5, or 25 hours, depending on what time zone and whether DST is starting or stopping on that day. So in the time-of-day context, you probably do want to include a particular date and zone in your calculation. Of course, if you are talking about straight 24-hours days, then this point is irrelevant.

    If you are talking about durations - you might want to look again at moment.js, but not at the moment object. There is another object there, moment.duration. The reference is here.

    And finally, you might want to consider just using plain javascript to parse out hours and minutes from the time string as numbers. Manipulate the numbers as necessary, and then output a string again. But your question seems like you're looking for something more managed.

    0 讨论(0)
提交回复
热议问题