Convert HH:MM:SS string to seconds only in javascript












62















I am having similar requirement as this: Convert time in HH:MM:SS format to seconds only?



but in javascript. I have seen many examples of converting seconds into different formats but not HH:MM:SS into seconds. Any help would be appreciated.










share|improve this question

























  • did you mean HH:MM:SS?

    – MilkyWayJoe
    Mar 9 '12 at 20:10













  • oops.. sorry for the typo. yes, it's time hh:mm:ss

    – Sri Reddy
    Mar 9 '12 at 20:10











  • What is the DD in HH:MM:DD?

    – Brian Driscoll
    Mar 9 '12 at 20:10






  • 2





    It's the same algorithm as is in that PHP question.

    – scottheckel
    Mar 9 '12 at 20:11






  • 1





    Only solution I think of is to split the string in array and then multiply 3600 to hour and multiply 60 to min and add all with seconds part. Is this is the simplest solution?

    – Sri Reddy
    Mar 9 '12 at 20:13
















62















I am having similar requirement as this: Convert time in HH:MM:SS format to seconds only?



but in javascript. I have seen many examples of converting seconds into different formats but not HH:MM:SS into seconds. Any help would be appreciated.










share|improve this question

























  • did you mean HH:MM:SS?

    – MilkyWayJoe
    Mar 9 '12 at 20:10













  • oops.. sorry for the typo. yes, it's time hh:mm:ss

    – Sri Reddy
    Mar 9 '12 at 20:10











  • What is the DD in HH:MM:DD?

    – Brian Driscoll
    Mar 9 '12 at 20:10






  • 2





    It's the same algorithm as is in that PHP question.

    – scottheckel
    Mar 9 '12 at 20:11






  • 1





    Only solution I think of is to split the string in array and then multiply 3600 to hour and multiply 60 to min and add all with seconds part. Is this is the simplest solution?

    – Sri Reddy
    Mar 9 '12 at 20:13














62












62








62


26






I am having similar requirement as this: Convert time in HH:MM:SS format to seconds only?



but in javascript. I have seen many examples of converting seconds into different formats but not HH:MM:SS into seconds. Any help would be appreciated.










share|improve this question
















I am having similar requirement as this: Convert time in HH:MM:SS format to seconds only?



but in javascript. I have seen many examples of converting seconds into different formats but not HH:MM:SS into seconds. Any help would be appreciated.







javascript time






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 23 '17 at 11:54









Community

11




11










asked Mar 9 '12 at 20:06









Sri ReddySri Reddy

2,398175598




2,398175598













  • did you mean HH:MM:SS?

    – MilkyWayJoe
    Mar 9 '12 at 20:10













  • oops.. sorry for the typo. yes, it's time hh:mm:ss

    – Sri Reddy
    Mar 9 '12 at 20:10











  • What is the DD in HH:MM:DD?

    – Brian Driscoll
    Mar 9 '12 at 20:10






  • 2





    It's the same algorithm as is in that PHP question.

    – scottheckel
    Mar 9 '12 at 20:11






  • 1





    Only solution I think of is to split the string in array and then multiply 3600 to hour and multiply 60 to min and add all with seconds part. Is this is the simplest solution?

    – Sri Reddy
    Mar 9 '12 at 20:13



















  • did you mean HH:MM:SS?

    – MilkyWayJoe
    Mar 9 '12 at 20:10













  • oops.. sorry for the typo. yes, it's time hh:mm:ss

    – Sri Reddy
    Mar 9 '12 at 20:10











  • What is the DD in HH:MM:DD?

    – Brian Driscoll
    Mar 9 '12 at 20:10






  • 2





    It's the same algorithm as is in that PHP question.

    – scottheckel
    Mar 9 '12 at 20:11






  • 1





    Only solution I think of is to split the string in array and then multiply 3600 to hour and multiply 60 to min and add all with seconds part. Is this is the simplest solution?

    – Sri Reddy
    Mar 9 '12 at 20:13

















did you mean HH:MM:SS?

– MilkyWayJoe
Mar 9 '12 at 20:10







did you mean HH:MM:SS?

– MilkyWayJoe
Mar 9 '12 at 20:10















oops.. sorry for the typo. yes, it's time hh:mm:ss

– Sri Reddy
Mar 9 '12 at 20:10





oops.. sorry for the typo. yes, it's time hh:mm:ss

– Sri Reddy
Mar 9 '12 at 20:10













What is the DD in HH:MM:DD?

– Brian Driscoll
Mar 9 '12 at 20:10





What is the DD in HH:MM:DD?

– Brian Driscoll
Mar 9 '12 at 20:10




2




2





It's the same algorithm as is in that PHP question.

– scottheckel
Mar 9 '12 at 20:11





It's the same algorithm as is in that PHP question.

– scottheckel
Mar 9 '12 at 20:11




1




1





Only solution I think of is to split the string in array and then multiply 3600 to hour and multiply 60 to min and add all with seconds part. Is this is the simplest solution?

– Sri Reddy
Mar 9 '12 at 20:13





Only solution I think of is to split the string in array and then multiply 3600 to hour and multiply 60 to min and add all with seconds part. Is this is the simplest solution?

– Sri Reddy
Mar 9 '12 at 20:13












11 Answers
11






active

oldest

votes


















137














Try this:



var hms = '02:04:33';   // your input string
var a = hms.split(':'); // split it at the colons

// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);

console.log(seconds);





share|improve this answer



















  • 2





    Here's a prototype version of this! String.prototype.toSeconds = function () { if (!this) return null; var hms = this.split(':'); return (+hms[0]) * 60 * 60 + (+hms[1]) * 60 + (+hms[2] || 0); } NOTE: the || 0 at the end is good for any implementation of this code - it prevents issues with a (still valid) time representation of HH:MM (Chrome type="time" inputs will output in this format when seconds=0).

    – Fateh Khalsa
    May 6 '16 at 0:47













  • Awesome stuff dude

    – edward
    Nov 30 '16 at 15:25











  • i have only hh:mm not seconds in this case what i have to modify..?

    – Mr world wide
    Dec 21 '16 at 6:02






  • 1





    @AbdulWaheed Change this line: var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); to var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60;

    – Rova
    Mar 2 '17 at 16:05






  • 2





    The use of "+" operator to convert numeric string to number is not a good idea. It's short and looks "clever", but it's confusing and not clean code. Use parseInt(x, 10) instead. And avoid one-liner. Also prevent errors by undefined input. For example: it's not a string, has no ":" or only "HH:MM". etc.

    – Dominik
    Sep 26 '17 at 14:09



















52














This function handels "HH:MM:SS" as well as "MM:SS" or "SS".



function hmsToSecondsOnly(str) {
var p = str.split(':'),
s = 0, m = 1;

while (p.length > 0) {
s += m * parseInt(p.pop(), 10);
m *= 60;
}

return s;
}





share|improve this answer


























  • Nice, I will keep it if requirement comes up for time passed is any format. Thanks!

    – Sri Reddy
    Mar 9 '12 at 20:27











  • Works perfect !

    – Shan Eapen Koshy
    Sep 4 '15 at 17:34











  • Best answer for me so far

    – Rezwan Azfar Haleem
    Nov 14 '16 at 1:49











  • This answer is useful

    – Mike Aron
    Sep 8 '18 at 18:02



















23














This can be done quite resiliently with the following:



'01:02:03'.split(':').reduce((acc,time) => (60 * acc) + +time);


This is because each unit of time within the hours, minutes and seconds is a multiple of 60 greater than the smaller unit. Time is split into hour minutes and seconds components, then reduced to seconds by using the accumulated value of the higher units multiplied by 60 as it goes through each unit.



The +time is used to cast the time to a number.



It basically ends up doing: (60 * ((60 * HHHH) + MM)) + SS






share|improve this answer
























  • thats actually pretty damn clever.. question is how fast this is compared to a calculation

    – ThatBrianDude
    Sep 20 '17 at 17:35








  • 1





    Yes it's clever, but a good example of creating difficult to maintain code for no practical gain. It saves 4 characters when minified vs a minified version of the accepted answer. Given that many web pages are now in excess of 1MB, that saving is somewhat less than insignificant.

    – RobG
    May 19 '18 at 9:24






  • 2





    Although this answer isn't as comprehensible, it does gracefully handle both HH:MM:SS as well as MM:SS, while the accepted answer does not.

    – ckeeney
    Aug 31 '18 at 18:45



















9














Convert hh:mm:ss string to seconds in one line. Also allowed h:m:s format and mm:ss, m:s etc



'08:45:20'.split(':').reverse().reduce((prev, curr, i) => prev + curr*Math.pow(60, i), 0)





share|improve this answer





















  • 4





    Please explain your answer

    – B001ᛦ
    Nov 10 '16 at 13:43











  • Convert hh:mm:ss string to seconds in one line. Also allowed h:m:s format and mm:ss, m:s etc.

    – Paul Hide
    Nov 25 '16 at 12:56











  • Nice answer! But you could skip the reverse(): '00:01:11'.split(':').reduce((val, entry, i) => val + entry * (3600/Math.pow(60, i)), 0) === 71

    – CMR
    Jan 7 '17 at 20:55











  • @CMR, interesting aproach, but in case of mm:ss it will not work correctly.

    – Paul Hide
    Jan 9 '17 at 13:43





















7














Since the getTime function of the Date object gets the milliseconds since 1970/01/01, we can do this:



var time = '12:23:00';
var seconds = new Date('1970-01-01T' + time + 'Z').getTime() / 1000;





share|improve this answer





















  • 1





    Just realized, this doesnt work with daylight savings time. Need to use the actual date

    – Yablargo
    Jun 13 '13 at 3:08











  • @Yablargo Thanks. Previous version didn't work very well with local timezone, so i edited it to use iso 8601 utc datetime format.

    – boskop
    Jul 15 '13 at 10:30



















6














try



time="12:12:12";
tt=time.split(":");
sec=tt[0]*3600+tt[1]*60+tt[2]*1;





share|improve this answer



















  • 4





    ah, that *1 is a clever way to get it not to do string concatenation :)

    – Dagg Nabbit
    Mar 9 '12 at 20:22



















4














Javascript's static method Date.UTC() does the trick:



alert(getSeconds('00:22:17'));

function getSeconds(time)
{
var ts = time.split(':');
return Date.UTC(1970, 0, 1, ts[0], ts[1], ts[2]) / 1000;
}





share|improve this answer































    2














    new Date(moment('23:04:33', "HH:mm")).getTime()


    Output: 1499755980000 (in millisecond)
    ( 1499755980000/1000) (in second)



    Note : this output calculate diff from 1970-01-01 12:0:0 to now
    and we need to implement the moment.js






    share|improve this answer


























    • OP asked for seconds not milliseconds

      – user7294900
      Jul 11 '17 at 7:51











    • Hi user7294900, Thx for your comment i ll update my answer, jst we need divide by 1000

      – ITsDEv
      Jul 27 '17 at 8:06



















    1














    This function works for MM:SS as well:






    const convertTime = (hms) => {
    if (hms.length <3){
    return hms
    } else if (hms.length <6){
    const a = hms.split(':')
    return hms = (+a[0]) * 60 + (+a[1])
    } else {
    const a = hms.split(':')
    return hms = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2])
    }
    }








    share|improve this answer































      0

















      function parsehhmmsst(arg) {
      var result = 0, arr = arg.split(':')
      if (arr[0] < 12) {
      result = arr[0] * 3600 // hours
      }
      result += arr[1] * 60 // minutes
      result += parseInt(arr[2]) // seconds
      if (arg.indexOf('P') > -1) { // 8:00 PM > 8:00 AM
      result += 43200
      }
      return result
      }
      $('body').append(parsehhmmsst('12:00:00 AM') + '<br>')
      $('body').append(parsehhmmsst('1:00:00 AM') + '<br>')
      $('body').append(parsehhmmsst('2:00:00 AM') + '<br>')
      $('body').append(parsehhmmsst('3:00:00 AM') + '<br>')
      $('body').append(parsehhmmsst('4:00:00 AM') + '<br>')
      $('body').append(parsehhmmsst('5:00:00 AM') + '<br>')
      $('body').append(parsehhmmsst('6:00:00 AM') + '<br>')
      $('body').append(parsehhmmsst('7:00:00 AM') + '<br>')
      $('body').append(parsehhmmsst('8:00:00 AM') + '<br>')
      $('body').append(parsehhmmsst('9:00:00 AM') + '<br>')
      $('body').append(parsehhmmsst('10:00:00 AM') + '<br>')
      $('body').append(parsehhmmsst('11:00:00 AM') + '<br>')
      $('body').append(parsehhmmsst('12:00:00 PM') + '<br>')
      $('body').append(parsehhmmsst('1:00:00 PM') + '<br>')
      $('body').append(parsehhmmsst('2:00:00 PM') + '<br>')
      $('body').append(parsehhmmsst('3:00:00 PM') + '<br>')
      $('body').append(parsehhmmsst('4:00:00 PM') + '<br>')
      $('body').append(parsehhmmsst('5:00:00 PM') + '<br>')
      $('body').append(parsehhmmsst('6:00:00 PM') + '<br>')
      $('body').append(parsehhmmsst('7:00:00 PM') + '<br>')
      $('body').append(parsehhmmsst('8:00:00 PM') + '<br>')
      $('body').append(parsehhmmsst('9:00:00 PM') + '<br>')
      $('body').append(parsehhmmsst('10:00:00 PM') + '<br>')
      $('body').append(parsehhmmsst('11:00:00 PM') + '<br>')

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>








      share|improve this answer































        0














        You can do this dynamically - in case you encounter not only: HH:mm:ss, but also, mm:ss, or even ss alone.



        var str = '12:99:07';
        var times = str.split(":");
        times.reverse();
        var x = times.length, y = 0, z;
        for (var i = 0; i < x; i++) {
        z = times[i] * Math.pow(60, i);
        y += z;
        }
        console.log(y);





        share|improve this answer

























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9640266%2fconvert-hhmmss-string-to-seconds-only-in-javascript%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          11 Answers
          11






          active

          oldest

          votes








          11 Answers
          11






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          137














          Try this:



          var hms = '02:04:33';   // your input string
          var a = hms.split(':'); // split it at the colons

          // minutes are worth 60 seconds. Hours are worth 60 minutes.
          var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);

          console.log(seconds);





          share|improve this answer



















          • 2





            Here's a prototype version of this! String.prototype.toSeconds = function () { if (!this) return null; var hms = this.split(':'); return (+hms[0]) * 60 * 60 + (+hms[1]) * 60 + (+hms[2] || 0); } NOTE: the || 0 at the end is good for any implementation of this code - it prevents issues with a (still valid) time representation of HH:MM (Chrome type="time" inputs will output in this format when seconds=0).

            – Fateh Khalsa
            May 6 '16 at 0:47













          • Awesome stuff dude

            – edward
            Nov 30 '16 at 15:25











          • i have only hh:mm not seconds in this case what i have to modify..?

            – Mr world wide
            Dec 21 '16 at 6:02






          • 1





            @AbdulWaheed Change this line: var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); to var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60;

            – Rova
            Mar 2 '17 at 16:05






          • 2





            The use of "+" operator to convert numeric string to number is not a good idea. It's short and looks "clever", but it's confusing and not clean code. Use parseInt(x, 10) instead. And avoid one-liner. Also prevent errors by undefined input. For example: it's not a string, has no ":" or only "HH:MM". etc.

            – Dominik
            Sep 26 '17 at 14:09
















          137














          Try this:



          var hms = '02:04:33';   // your input string
          var a = hms.split(':'); // split it at the colons

          // minutes are worth 60 seconds. Hours are worth 60 minutes.
          var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);

          console.log(seconds);





          share|improve this answer



















          • 2





            Here's a prototype version of this! String.prototype.toSeconds = function () { if (!this) return null; var hms = this.split(':'); return (+hms[0]) * 60 * 60 + (+hms[1]) * 60 + (+hms[2] || 0); } NOTE: the || 0 at the end is good for any implementation of this code - it prevents issues with a (still valid) time representation of HH:MM (Chrome type="time" inputs will output in this format when seconds=0).

            – Fateh Khalsa
            May 6 '16 at 0:47













          • Awesome stuff dude

            – edward
            Nov 30 '16 at 15:25











          • i have only hh:mm not seconds in this case what i have to modify..?

            – Mr world wide
            Dec 21 '16 at 6:02






          • 1





            @AbdulWaheed Change this line: var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); to var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60;

            – Rova
            Mar 2 '17 at 16:05






          • 2





            The use of "+" operator to convert numeric string to number is not a good idea. It's short and looks "clever", but it's confusing and not clean code. Use parseInt(x, 10) instead. And avoid one-liner. Also prevent errors by undefined input. For example: it's not a string, has no ":" or only "HH:MM". etc.

            – Dominik
            Sep 26 '17 at 14:09














          137












          137








          137







          Try this:



          var hms = '02:04:33';   // your input string
          var a = hms.split(':'); // split it at the colons

          // minutes are worth 60 seconds. Hours are worth 60 minutes.
          var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);

          console.log(seconds);





          share|improve this answer













          Try this:



          var hms = '02:04:33';   // your input string
          var a = hms.split(':'); // split it at the colons

          // minutes are worth 60 seconds. Hours are worth 60 minutes.
          var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);

          console.log(seconds);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 9 '12 at 20:15









          Dagg NabbitDagg Nabbit

          50.8k1686131




          50.8k1686131








          • 2





            Here's a prototype version of this! String.prototype.toSeconds = function () { if (!this) return null; var hms = this.split(':'); return (+hms[0]) * 60 * 60 + (+hms[1]) * 60 + (+hms[2] || 0); } NOTE: the || 0 at the end is good for any implementation of this code - it prevents issues with a (still valid) time representation of HH:MM (Chrome type="time" inputs will output in this format when seconds=0).

            – Fateh Khalsa
            May 6 '16 at 0:47













          • Awesome stuff dude

            – edward
            Nov 30 '16 at 15:25











          • i have only hh:mm not seconds in this case what i have to modify..?

            – Mr world wide
            Dec 21 '16 at 6:02






          • 1





            @AbdulWaheed Change this line: var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); to var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60;

            – Rova
            Mar 2 '17 at 16:05






          • 2





            The use of "+" operator to convert numeric string to number is not a good idea. It's short and looks "clever", but it's confusing and not clean code. Use parseInt(x, 10) instead. And avoid one-liner. Also prevent errors by undefined input. For example: it's not a string, has no ":" or only "HH:MM". etc.

            – Dominik
            Sep 26 '17 at 14:09














          • 2





            Here's a prototype version of this! String.prototype.toSeconds = function () { if (!this) return null; var hms = this.split(':'); return (+hms[0]) * 60 * 60 + (+hms[1]) * 60 + (+hms[2] || 0); } NOTE: the || 0 at the end is good for any implementation of this code - it prevents issues with a (still valid) time representation of HH:MM (Chrome type="time" inputs will output in this format when seconds=0).

            – Fateh Khalsa
            May 6 '16 at 0:47













          • Awesome stuff dude

            – edward
            Nov 30 '16 at 15:25











          • i have only hh:mm not seconds in this case what i have to modify..?

            – Mr world wide
            Dec 21 '16 at 6:02






          • 1





            @AbdulWaheed Change this line: var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); to var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60;

            – Rova
            Mar 2 '17 at 16:05






          • 2





            The use of "+" operator to convert numeric string to number is not a good idea. It's short and looks "clever", but it's confusing and not clean code. Use parseInt(x, 10) instead. And avoid one-liner. Also prevent errors by undefined input. For example: it's not a string, has no ":" or only "HH:MM". etc.

            – Dominik
            Sep 26 '17 at 14:09








          2




          2





          Here's a prototype version of this! String.prototype.toSeconds = function () { if (!this) return null; var hms = this.split(':'); return (+hms[0]) * 60 * 60 + (+hms[1]) * 60 + (+hms[2] || 0); } NOTE: the || 0 at the end is good for any implementation of this code - it prevents issues with a (still valid) time representation of HH:MM (Chrome type="time" inputs will output in this format when seconds=0).

          – Fateh Khalsa
          May 6 '16 at 0:47







          Here's a prototype version of this! String.prototype.toSeconds = function () { if (!this) return null; var hms = this.split(':'); return (+hms[0]) * 60 * 60 + (+hms[1]) * 60 + (+hms[2] || 0); } NOTE: the || 0 at the end is good for any implementation of this code - it prevents issues with a (still valid) time representation of HH:MM (Chrome type="time" inputs will output in this format when seconds=0).

          – Fateh Khalsa
          May 6 '16 at 0:47















          Awesome stuff dude

          – edward
          Nov 30 '16 at 15:25





          Awesome stuff dude

          – edward
          Nov 30 '16 at 15:25













          i have only hh:mm not seconds in this case what i have to modify..?

          – Mr world wide
          Dec 21 '16 at 6:02





          i have only hh:mm not seconds in this case what i have to modify..?

          – Mr world wide
          Dec 21 '16 at 6:02




          1




          1





          @AbdulWaheed Change this line: var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); to var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60;

          – Rova
          Mar 2 '17 at 16:05





          @AbdulWaheed Change this line: var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); to var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60;

          – Rova
          Mar 2 '17 at 16:05




          2




          2





          The use of "+" operator to convert numeric string to number is not a good idea. It's short and looks "clever", but it's confusing and not clean code. Use parseInt(x, 10) instead. And avoid one-liner. Also prevent errors by undefined input. For example: it's not a string, has no ":" or only "HH:MM". etc.

          – Dominik
          Sep 26 '17 at 14:09





          The use of "+" operator to convert numeric string to number is not a good idea. It's short and looks "clever", but it's confusing and not clean code. Use parseInt(x, 10) instead. And avoid one-liner. Also prevent errors by undefined input. For example: it's not a string, has no ":" or only "HH:MM". etc.

          – Dominik
          Sep 26 '17 at 14:09













          52














          This function handels "HH:MM:SS" as well as "MM:SS" or "SS".



          function hmsToSecondsOnly(str) {
          var p = str.split(':'),
          s = 0, m = 1;

          while (p.length > 0) {
          s += m * parseInt(p.pop(), 10);
          m *= 60;
          }

          return s;
          }





          share|improve this answer


























          • Nice, I will keep it if requirement comes up for time passed is any format. Thanks!

            – Sri Reddy
            Mar 9 '12 at 20:27











          • Works perfect !

            – Shan Eapen Koshy
            Sep 4 '15 at 17:34











          • Best answer for me so far

            – Rezwan Azfar Haleem
            Nov 14 '16 at 1:49











          • This answer is useful

            – Mike Aron
            Sep 8 '18 at 18:02
















          52














          This function handels "HH:MM:SS" as well as "MM:SS" or "SS".



          function hmsToSecondsOnly(str) {
          var p = str.split(':'),
          s = 0, m = 1;

          while (p.length > 0) {
          s += m * parseInt(p.pop(), 10);
          m *= 60;
          }

          return s;
          }





          share|improve this answer


























          • Nice, I will keep it if requirement comes up for time passed is any format. Thanks!

            – Sri Reddy
            Mar 9 '12 at 20:27











          • Works perfect !

            – Shan Eapen Koshy
            Sep 4 '15 at 17:34











          • Best answer for me so far

            – Rezwan Azfar Haleem
            Nov 14 '16 at 1:49











          • This answer is useful

            – Mike Aron
            Sep 8 '18 at 18:02














          52












          52








          52







          This function handels "HH:MM:SS" as well as "MM:SS" or "SS".



          function hmsToSecondsOnly(str) {
          var p = str.split(':'),
          s = 0, m = 1;

          while (p.length > 0) {
          s += m * parseInt(p.pop(), 10);
          m *= 60;
          }

          return s;
          }





          share|improve this answer















          This function handels "HH:MM:SS" as well as "MM:SS" or "SS".



          function hmsToSecondsOnly(str) {
          var p = str.split(':'),
          s = 0, m = 1;

          while (p.length > 0) {
          s += m * parseInt(p.pop(), 10);
          m *= 60;
          }

          return s;
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 12 '13 at 19:33

























          answered Mar 9 '12 at 20:18









          NikoNiko

          23.4k774105




          23.4k774105













          • Nice, I will keep it if requirement comes up for time passed is any format. Thanks!

            – Sri Reddy
            Mar 9 '12 at 20:27











          • Works perfect !

            – Shan Eapen Koshy
            Sep 4 '15 at 17:34











          • Best answer for me so far

            – Rezwan Azfar Haleem
            Nov 14 '16 at 1:49











          • This answer is useful

            – Mike Aron
            Sep 8 '18 at 18:02



















          • Nice, I will keep it if requirement comes up for time passed is any format. Thanks!

            – Sri Reddy
            Mar 9 '12 at 20:27











          • Works perfect !

            – Shan Eapen Koshy
            Sep 4 '15 at 17:34











          • Best answer for me so far

            – Rezwan Azfar Haleem
            Nov 14 '16 at 1:49











          • This answer is useful

            – Mike Aron
            Sep 8 '18 at 18:02

















          Nice, I will keep it if requirement comes up for time passed is any format. Thanks!

          – Sri Reddy
          Mar 9 '12 at 20:27





          Nice, I will keep it if requirement comes up for time passed is any format. Thanks!

          – Sri Reddy
          Mar 9 '12 at 20:27













          Works perfect !

          – Shan Eapen Koshy
          Sep 4 '15 at 17:34





          Works perfect !

          – Shan Eapen Koshy
          Sep 4 '15 at 17:34













          Best answer for me so far

          – Rezwan Azfar Haleem
          Nov 14 '16 at 1:49





          Best answer for me so far

          – Rezwan Azfar Haleem
          Nov 14 '16 at 1:49













          This answer is useful

          – Mike Aron
          Sep 8 '18 at 18:02





          This answer is useful

          – Mike Aron
          Sep 8 '18 at 18:02











          23














          This can be done quite resiliently with the following:



          '01:02:03'.split(':').reduce((acc,time) => (60 * acc) + +time);


          This is because each unit of time within the hours, minutes and seconds is a multiple of 60 greater than the smaller unit. Time is split into hour minutes and seconds components, then reduced to seconds by using the accumulated value of the higher units multiplied by 60 as it goes through each unit.



          The +time is used to cast the time to a number.



          It basically ends up doing: (60 * ((60 * HHHH) + MM)) + SS






          share|improve this answer
























          • thats actually pretty damn clever.. question is how fast this is compared to a calculation

            – ThatBrianDude
            Sep 20 '17 at 17:35








          • 1





            Yes it's clever, but a good example of creating difficult to maintain code for no practical gain. It saves 4 characters when minified vs a minified version of the accepted answer. Given that many web pages are now in excess of 1MB, that saving is somewhat less than insignificant.

            – RobG
            May 19 '18 at 9:24






          • 2





            Although this answer isn't as comprehensible, it does gracefully handle both HH:MM:SS as well as MM:SS, while the accepted answer does not.

            – ckeeney
            Aug 31 '18 at 18:45
















          23














          This can be done quite resiliently with the following:



          '01:02:03'.split(':').reduce((acc,time) => (60 * acc) + +time);


          This is because each unit of time within the hours, minutes and seconds is a multiple of 60 greater than the smaller unit. Time is split into hour minutes and seconds components, then reduced to seconds by using the accumulated value of the higher units multiplied by 60 as it goes through each unit.



          The +time is used to cast the time to a number.



          It basically ends up doing: (60 * ((60 * HHHH) + MM)) + SS






          share|improve this answer
























          • thats actually pretty damn clever.. question is how fast this is compared to a calculation

            – ThatBrianDude
            Sep 20 '17 at 17:35








          • 1





            Yes it's clever, but a good example of creating difficult to maintain code for no practical gain. It saves 4 characters when minified vs a minified version of the accepted answer. Given that many web pages are now in excess of 1MB, that saving is somewhat less than insignificant.

            – RobG
            May 19 '18 at 9:24






          • 2





            Although this answer isn't as comprehensible, it does gracefully handle both HH:MM:SS as well as MM:SS, while the accepted answer does not.

            – ckeeney
            Aug 31 '18 at 18:45














          23












          23








          23







          This can be done quite resiliently with the following:



          '01:02:03'.split(':').reduce((acc,time) => (60 * acc) + +time);


          This is because each unit of time within the hours, minutes and seconds is a multiple of 60 greater than the smaller unit. Time is split into hour minutes and seconds components, then reduced to seconds by using the accumulated value of the higher units multiplied by 60 as it goes through each unit.



          The +time is used to cast the time to a number.



          It basically ends up doing: (60 * ((60 * HHHH) + MM)) + SS






          share|improve this answer













          This can be done quite resiliently with the following:



          '01:02:03'.split(':').reduce((acc,time) => (60 * acc) + +time);


          This is because each unit of time within the hours, minutes and seconds is a multiple of 60 greater than the smaller unit. Time is split into hour minutes and seconds components, then reduced to seconds by using the accumulated value of the higher units multiplied by 60 as it goes through each unit.



          The +time is used to cast the time to a number.



          It basically ends up doing: (60 * ((60 * HHHH) + MM)) + SS







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 25 '17 at 1:49









          PaulPaul

          4,1172841




          4,1172841













          • thats actually pretty damn clever.. question is how fast this is compared to a calculation

            – ThatBrianDude
            Sep 20 '17 at 17:35








          • 1





            Yes it's clever, but a good example of creating difficult to maintain code for no practical gain. It saves 4 characters when minified vs a minified version of the accepted answer. Given that many web pages are now in excess of 1MB, that saving is somewhat less than insignificant.

            – RobG
            May 19 '18 at 9:24






          • 2





            Although this answer isn't as comprehensible, it does gracefully handle both HH:MM:SS as well as MM:SS, while the accepted answer does not.

            – ckeeney
            Aug 31 '18 at 18:45



















          • thats actually pretty damn clever.. question is how fast this is compared to a calculation

            – ThatBrianDude
            Sep 20 '17 at 17:35








          • 1





            Yes it's clever, but a good example of creating difficult to maintain code for no practical gain. It saves 4 characters when minified vs a minified version of the accepted answer. Given that many web pages are now in excess of 1MB, that saving is somewhat less than insignificant.

            – RobG
            May 19 '18 at 9:24






          • 2





            Although this answer isn't as comprehensible, it does gracefully handle both HH:MM:SS as well as MM:SS, while the accepted answer does not.

            – ckeeney
            Aug 31 '18 at 18:45

















          thats actually pretty damn clever.. question is how fast this is compared to a calculation

          – ThatBrianDude
          Sep 20 '17 at 17:35







          thats actually pretty damn clever.. question is how fast this is compared to a calculation

          – ThatBrianDude
          Sep 20 '17 at 17:35






          1




          1





          Yes it's clever, but a good example of creating difficult to maintain code for no practical gain. It saves 4 characters when minified vs a minified version of the accepted answer. Given that many web pages are now in excess of 1MB, that saving is somewhat less than insignificant.

          – RobG
          May 19 '18 at 9:24





          Yes it's clever, but a good example of creating difficult to maintain code for no practical gain. It saves 4 characters when minified vs a minified version of the accepted answer. Given that many web pages are now in excess of 1MB, that saving is somewhat less than insignificant.

          – RobG
          May 19 '18 at 9:24




          2




          2





          Although this answer isn't as comprehensible, it does gracefully handle both HH:MM:SS as well as MM:SS, while the accepted answer does not.

          – ckeeney
          Aug 31 '18 at 18:45





          Although this answer isn't as comprehensible, it does gracefully handle both HH:MM:SS as well as MM:SS, while the accepted answer does not.

          – ckeeney
          Aug 31 '18 at 18:45











          9














          Convert hh:mm:ss string to seconds in one line. Also allowed h:m:s format and mm:ss, m:s etc



          '08:45:20'.split(':').reverse().reduce((prev, curr, i) => prev + curr*Math.pow(60, i), 0)





          share|improve this answer





















          • 4





            Please explain your answer

            – B001ᛦ
            Nov 10 '16 at 13:43











          • Convert hh:mm:ss string to seconds in one line. Also allowed h:m:s format and mm:ss, m:s etc.

            – Paul Hide
            Nov 25 '16 at 12:56











          • Nice answer! But you could skip the reverse(): '00:01:11'.split(':').reduce((val, entry, i) => val + entry * (3600/Math.pow(60, i)), 0) === 71

            – CMR
            Jan 7 '17 at 20:55











          • @CMR, interesting aproach, but in case of mm:ss it will not work correctly.

            – Paul Hide
            Jan 9 '17 at 13:43


















          9














          Convert hh:mm:ss string to seconds in one line. Also allowed h:m:s format and mm:ss, m:s etc



          '08:45:20'.split(':').reverse().reduce((prev, curr, i) => prev + curr*Math.pow(60, i), 0)





          share|improve this answer





















          • 4





            Please explain your answer

            – B001ᛦ
            Nov 10 '16 at 13:43











          • Convert hh:mm:ss string to seconds in one line. Also allowed h:m:s format and mm:ss, m:s etc.

            – Paul Hide
            Nov 25 '16 at 12:56











          • Nice answer! But you could skip the reverse(): '00:01:11'.split(':').reduce((val, entry, i) => val + entry * (3600/Math.pow(60, i)), 0) === 71

            – CMR
            Jan 7 '17 at 20:55











          • @CMR, interesting aproach, but in case of mm:ss it will not work correctly.

            – Paul Hide
            Jan 9 '17 at 13:43
















          9












          9








          9







          Convert hh:mm:ss string to seconds in one line. Also allowed h:m:s format and mm:ss, m:s etc



          '08:45:20'.split(':').reverse().reduce((prev, curr, i) => prev + curr*Math.pow(60, i), 0)





          share|improve this answer















          Convert hh:mm:ss string to seconds in one line. Also allowed h:m:s format and mm:ss, m:s etc



          '08:45:20'.split(':').reverse().reduce((prev, curr, i) => prev + curr*Math.pow(60, i), 0)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 25 '16 at 14:34









          B001ᛦ

          1,13951320




          1,13951320










          answered Nov 10 '16 at 13:35









          Paul HidePaul Hide

          10612




          10612








          • 4





            Please explain your answer

            – B001ᛦ
            Nov 10 '16 at 13:43











          • Convert hh:mm:ss string to seconds in one line. Also allowed h:m:s format and mm:ss, m:s etc.

            – Paul Hide
            Nov 25 '16 at 12:56











          • Nice answer! But you could skip the reverse(): '00:01:11'.split(':').reduce((val, entry, i) => val + entry * (3600/Math.pow(60, i)), 0) === 71

            – CMR
            Jan 7 '17 at 20:55











          • @CMR, interesting aproach, but in case of mm:ss it will not work correctly.

            – Paul Hide
            Jan 9 '17 at 13:43
















          • 4





            Please explain your answer

            – B001ᛦ
            Nov 10 '16 at 13:43











          • Convert hh:mm:ss string to seconds in one line. Also allowed h:m:s format and mm:ss, m:s etc.

            – Paul Hide
            Nov 25 '16 at 12:56











          • Nice answer! But you could skip the reverse(): '00:01:11'.split(':').reduce((val, entry, i) => val + entry * (3600/Math.pow(60, i)), 0) === 71

            – CMR
            Jan 7 '17 at 20:55











          • @CMR, interesting aproach, but in case of mm:ss it will not work correctly.

            – Paul Hide
            Jan 9 '17 at 13:43










          4




          4





          Please explain your answer

          – B001ᛦ
          Nov 10 '16 at 13:43





          Please explain your answer

          – B001ᛦ
          Nov 10 '16 at 13:43













          Convert hh:mm:ss string to seconds in one line. Also allowed h:m:s format and mm:ss, m:s etc.

          – Paul Hide
          Nov 25 '16 at 12:56





          Convert hh:mm:ss string to seconds in one line. Also allowed h:m:s format and mm:ss, m:s etc.

          – Paul Hide
          Nov 25 '16 at 12:56













          Nice answer! But you could skip the reverse(): '00:01:11'.split(':').reduce((val, entry, i) => val + entry * (3600/Math.pow(60, i)), 0) === 71

          – CMR
          Jan 7 '17 at 20:55





          Nice answer! But you could skip the reverse(): '00:01:11'.split(':').reduce((val, entry, i) => val + entry * (3600/Math.pow(60, i)), 0) === 71

          – CMR
          Jan 7 '17 at 20:55













          @CMR, interesting aproach, but in case of mm:ss it will not work correctly.

          – Paul Hide
          Jan 9 '17 at 13:43







          @CMR, interesting aproach, but in case of mm:ss it will not work correctly.

          – Paul Hide
          Jan 9 '17 at 13:43













          7














          Since the getTime function of the Date object gets the milliseconds since 1970/01/01, we can do this:



          var time = '12:23:00';
          var seconds = new Date('1970-01-01T' + time + 'Z').getTime() / 1000;





          share|improve this answer





















          • 1





            Just realized, this doesnt work with daylight savings time. Need to use the actual date

            – Yablargo
            Jun 13 '13 at 3:08











          • @Yablargo Thanks. Previous version didn't work very well with local timezone, so i edited it to use iso 8601 utc datetime format.

            – boskop
            Jul 15 '13 at 10:30
















          7














          Since the getTime function of the Date object gets the milliseconds since 1970/01/01, we can do this:



          var time = '12:23:00';
          var seconds = new Date('1970-01-01T' + time + 'Z').getTime() / 1000;





          share|improve this answer





















          • 1





            Just realized, this doesnt work with daylight savings time. Need to use the actual date

            – Yablargo
            Jun 13 '13 at 3:08











          • @Yablargo Thanks. Previous version didn't work very well with local timezone, so i edited it to use iso 8601 utc datetime format.

            – boskop
            Jul 15 '13 at 10:30














          7












          7








          7







          Since the getTime function of the Date object gets the milliseconds since 1970/01/01, we can do this:



          var time = '12:23:00';
          var seconds = new Date('1970-01-01T' + time + 'Z').getTime() / 1000;





          share|improve this answer















          Since the getTime function of the Date object gets the milliseconds since 1970/01/01, we can do this:



          var time = '12:23:00';
          var seconds = new Date('1970-01-01T' + time + 'Z').getTime() / 1000;






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jul 15 '13 at 10:24

























          answered Mar 15 '13 at 18:57









          boskopboskop

          31146




          31146








          • 1





            Just realized, this doesnt work with daylight savings time. Need to use the actual date

            – Yablargo
            Jun 13 '13 at 3:08











          • @Yablargo Thanks. Previous version didn't work very well with local timezone, so i edited it to use iso 8601 utc datetime format.

            – boskop
            Jul 15 '13 at 10:30














          • 1





            Just realized, this doesnt work with daylight savings time. Need to use the actual date

            – Yablargo
            Jun 13 '13 at 3:08











          • @Yablargo Thanks. Previous version didn't work very well with local timezone, so i edited it to use iso 8601 utc datetime format.

            – boskop
            Jul 15 '13 at 10:30








          1




          1





          Just realized, this doesnt work with daylight savings time. Need to use the actual date

          – Yablargo
          Jun 13 '13 at 3:08





          Just realized, this doesnt work with daylight savings time. Need to use the actual date

          – Yablargo
          Jun 13 '13 at 3:08













          @Yablargo Thanks. Previous version didn't work very well with local timezone, so i edited it to use iso 8601 utc datetime format.

          – boskop
          Jul 15 '13 at 10:30





          @Yablargo Thanks. Previous version didn't work very well with local timezone, so i edited it to use iso 8601 utc datetime format.

          – boskop
          Jul 15 '13 at 10:30











          6














          try



          time="12:12:12";
          tt=time.split(":");
          sec=tt[0]*3600+tt[1]*60+tt[2]*1;





          share|improve this answer



















          • 4





            ah, that *1 is a clever way to get it not to do string concatenation :)

            – Dagg Nabbit
            Mar 9 '12 at 20:22
















          6














          try



          time="12:12:12";
          tt=time.split(":");
          sec=tt[0]*3600+tt[1]*60+tt[2]*1;





          share|improve this answer



















          • 4





            ah, that *1 is a clever way to get it not to do string concatenation :)

            – Dagg Nabbit
            Mar 9 '12 at 20:22














          6












          6








          6







          try



          time="12:12:12";
          tt=time.split(":");
          sec=tt[0]*3600+tt[1]*60+tt[2]*1;





          share|improve this answer













          try



          time="12:12:12";
          tt=time.split(":");
          sec=tt[0]*3600+tt[1]*60+tt[2]*1;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 9 '12 at 20:16









          LucaLuca

          3,69811623




          3,69811623








          • 4





            ah, that *1 is a clever way to get it not to do string concatenation :)

            – Dagg Nabbit
            Mar 9 '12 at 20:22














          • 4





            ah, that *1 is a clever way to get it not to do string concatenation :)

            – Dagg Nabbit
            Mar 9 '12 at 20:22








          4




          4





          ah, that *1 is a clever way to get it not to do string concatenation :)

          – Dagg Nabbit
          Mar 9 '12 at 20:22





          ah, that *1 is a clever way to get it not to do string concatenation :)

          – Dagg Nabbit
          Mar 9 '12 at 20:22











          4














          Javascript's static method Date.UTC() does the trick:



          alert(getSeconds('00:22:17'));

          function getSeconds(time)
          {
          var ts = time.split(':');
          return Date.UTC(1970, 0, 1, ts[0], ts[1], ts[2]) / 1000;
          }





          share|improve this answer




























            4














            Javascript's static method Date.UTC() does the trick:



            alert(getSeconds('00:22:17'));

            function getSeconds(time)
            {
            var ts = time.split(':');
            return Date.UTC(1970, 0, 1, ts[0], ts[1], ts[2]) / 1000;
            }





            share|improve this answer


























              4












              4








              4







              Javascript's static method Date.UTC() does the trick:



              alert(getSeconds('00:22:17'));

              function getSeconds(time)
              {
              var ts = time.split(':');
              return Date.UTC(1970, 0, 1, ts[0], ts[1], ts[2]) / 1000;
              }





              share|improve this answer













              Javascript's static method Date.UTC() does the trick:



              alert(getSeconds('00:22:17'));

              function getSeconds(time)
              {
              var ts = time.split(':');
              return Date.UTC(1970, 0, 1, ts[0], ts[1], ts[2]) / 1000;
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jun 14 '13 at 8:06









              ChronozoaChronozoa

              195410




              195410























                  2














                  new Date(moment('23:04:33', "HH:mm")).getTime()


                  Output: 1499755980000 (in millisecond)
                  ( 1499755980000/1000) (in second)



                  Note : this output calculate diff from 1970-01-01 12:0:0 to now
                  and we need to implement the moment.js






                  share|improve this answer


























                  • OP asked for seconds not milliseconds

                    – user7294900
                    Jul 11 '17 at 7:51











                  • Hi user7294900, Thx for your comment i ll update my answer, jst we need divide by 1000

                    – ITsDEv
                    Jul 27 '17 at 8:06
















                  2














                  new Date(moment('23:04:33', "HH:mm")).getTime()


                  Output: 1499755980000 (in millisecond)
                  ( 1499755980000/1000) (in second)



                  Note : this output calculate diff from 1970-01-01 12:0:0 to now
                  and we need to implement the moment.js






                  share|improve this answer


























                  • OP asked for seconds not milliseconds

                    – user7294900
                    Jul 11 '17 at 7:51











                  • Hi user7294900, Thx for your comment i ll update my answer, jst we need divide by 1000

                    – ITsDEv
                    Jul 27 '17 at 8:06














                  2












                  2








                  2







                  new Date(moment('23:04:33', "HH:mm")).getTime()


                  Output: 1499755980000 (in millisecond)
                  ( 1499755980000/1000) (in second)



                  Note : this output calculate diff from 1970-01-01 12:0:0 to now
                  and we need to implement the moment.js






                  share|improve this answer















                  new Date(moment('23:04:33', "HH:mm")).getTime()


                  Output: 1499755980000 (in millisecond)
                  ( 1499755980000/1000) (in second)



                  Note : this output calculate diff from 1970-01-01 12:0:0 to now
                  and we need to implement the moment.js







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jul 27 '17 at 8:09

























                  answered Jul 11 '17 at 7:27









                  ITsDEvITsDEv

                  213




                  213













                  • OP asked for seconds not milliseconds

                    – user7294900
                    Jul 11 '17 at 7:51











                  • Hi user7294900, Thx for your comment i ll update my answer, jst we need divide by 1000

                    – ITsDEv
                    Jul 27 '17 at 8:06



















                  • OP asked for seconds not milliseconds

                    – user7294900
                    Jul 11 '17 at 7:51











                  • Hi user7294900, Thx for your comment i ll update my answer, jst we need divide by 1000

                    – ITsDEv
                    Jul 27 '17 at 8:06

















                  OP asked for seconds not milliseconds

                  – user7294900
                  Jul 11 '17 at 7:51





                  OP asked for seconds not milliseconds

                  – user7294900
                  Jul 11 '17 at 7:51













                  Hi user7294900, Thx for your comment i ll update my answer, jst we need divide by 1000

                  – ITsDEv
                  Jul 27 '17 at 8:06





                  Hi user7294900, Thx for your comment i ll update my answer, jst we need divide by 1000

                  – ITsDEv
                  Jul 27 '17 at 8:06











                  1














                  This function works for MM:SS as well:






                  const convertTime = (hms) => {
                  if (hms.length <3){
                  return hms
                  } else if (hms.length <6){
                  const a = hms.split(':')
                  return hms = (+a[0]) * 60 + (+a[1])
                  } else {
                  const a = hms.split(':')
                  return hms = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2])
                  }
                  }








                  share|improve this answer




























                    1














                    This function works for MM:SS as well:






                    const convertTime = (hms) => {
                    if (hms.length <3){
                    return hms
                    } else if (hms.length <6){
                    const a = hms.split(':')
                    return hms = (+a[0]) * 60 + (+a[1])
                    } else {
                    const a = hms.split(':')
                    return hms = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2])
                    }
                    }








                    share|improve this answer


























                      1












                      1








                      1







                      This function works for MM:SS as well:






                      const convertTime = (hms) => {
                      if (hms.length <3){
                      return hms
                      } else if (hms.length <6){
                      const a = hms.split(':')
                      return hms = (+a[0]) * 60 + (+a[1])
                      } else {
                      const a = hms.split(':')
                      return hms = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2])
                      }
                      }








                      share|improve this answer













                      This function works for MM:SS as well:






                      const convertTime = (hms) => {
                      if (hms.length <3){
                      return hms
                      } else if (hms.length <6){
                      const a = hms.split(':')
                      return hms = (+a[0]) * 60 + (+a[1])
                      } else {
                      const a = hms.split(':')
                      return hms = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2])
                      }
                      }








                      const convertTime = (hms) => {
                      if (hms.length <3){
                      return hms
                      } else if (hms.length <6){
                      const a = hms.split(':')
                      return hms = (+a[0]) * 60 + (+a[1])
                      } else {
                      const a = hms.split(':')
                      return hms = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2])
                      }
                      }





                      const convertTime = (hms) => {
                      if (hms.length <3){
                      return hms
                      } else if (hms.length <6){
                      const a = hms.split(':')
                      return hms = (+a[0]) * 60 + (+a[1])
                      } else {
                      const a = hms.split(':')
                      return hms = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2])
                      }
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 16 '17 at 8:19









                      Philip TranPhilip Tran

                      111




                      111























                          0

















                          function parsehhmmsst(arg) {
                          var result = 0, arr = arg.split(':')
                          if (arr[0] < 12) {
                          result = arr[0] * 3600 // hours
                          }
                          result += arr[1] * 60 // minutes
                          result += parseInt(arr[2]) // seconds
                          if (arg.indexOf('P') > -1) { // 8:00 PM > 8:00 AM
                          result += 43200
                          }
                          return result
                          }
                          $('body').append(parsehhmmsst('12:00:00 AM') + '<br>')
                          $('body').append(parsehhmmsst('1:00:00 AM') + '<br>')
                          $('body').append(parsehhmmsst('2:00:00 AM') + '<br>')
                          $('body').append(parsehhmmsst('3:00:00 AM') + '<br>')
                          $('body').append(parsehhmmsst('4:00:00 AM') + '<br>')
                          $('body').append(parsehhmmsst('5:00:00 AM') + '<br>')
                          $('body').append(parsehhmmsst('6:00:00 AM') + '<br>')
                          $('body').append(parsehhmmsst('7:00:00 AM') + '<br>')
                          $('body').append(parsehhmmsst('8:00:00 AM') + '<br>')
                          $('body').append(parsehhmmsst('9:00:00 AM') + '<br>')
                          $('body').append(parsehhmmsst('10:00:00 AM') + '<br>')
                          $('body').append(parsehhmmsst('11:00:00 AM') + '<br>')
                          $('body').append(parsehhmmsst('12:00:00 PM') + '<br>')
                          $('body').append(parsehhmmsst('1:00:00 PM') + '<br>')
                          $('body').append(parsehhmmsst('2:00:00 PM') + '<br>')
                          $('body').append(parsehhmmsst('3:00:00 PM') + '<br>')
                          $('body').append(parsehhmmsst('4:00:00 PM') + '<br>')
                          $('body').append(parsehhmmsst('5:00:00 PM') + '<br>')
                          $('body').append(parsehhmmsst('6:00:00 PM') + '<br>')
                          $('body').append(parsehhmmsst('7:00:00 PM') + '<br>')
                          $('body').append(parsehhmmsst('8:00:00 PM') + '<br>')
                          $('body').append(parsehhmmsst('9:00:00 PM') + '<br>')
                          $('body').append(parsehhmmsst('10:00:00 PM') + '<br>')
                          $('body').append(parsehhmmsst('11:00:00 PM') + '<br>')

                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>








                          share|improve this answer




























                            0

















                            function parsehhmmsst(arg) {
                            var result = 0, arr = arg.split(':')
                            if (arr[0] < 12) {
                            result = arr[0] * 3600 // hours
                            }
                            result += arr[1] * 60 // minutes
                            result += parseInt(arr[2]) // seconds
                            if (arg.indexOf('P') > -1) { // 8:00 PM > 8:00 AM
                            result += 43200
                            }
                            return result
                            }
                            $('body').append(parsehhmmsst('12:00:00 AM') + '<br>')
                            $('body').append(parsehhmmsst('1:00:00 AM') + '<br>')
                            $('body').append(parsehhmmsst('2:00:00 AM') + '<br>')
                            $('body').append(parsehhmmsst('3:00:00 AM') + '<br>')
                            $('body').append(parsehhmmsst('4:00:00 AM') + '<br>')
                            $('body').append(parsehhmmsst('5:00:00 AM') + '<br>')
                            $('body').append(parsehhmmsst('6:00:00 AM') + '<br>')
                            $('body').append(parsehhmmsst('7:00:00 AM') + '<br>')
                            $('body').append(parsehhmmsst('8:00:00 AM') + '<br>')
                            $('body').append(parsehhmmsst('9:00:00 AM') + '<br>')
                            $('body').append(parsehhmmsst('10:00:00 AM') + '<br>')
                            $('body').append(parsehhmmsst('11:00:00 AM') + '<br>')
                            $('body').append(parsehhmmsst('12:00:00 PM') + '<br>')
                            $('body').append(parsehhmmsst('1:00:00 PM') + '<br>')
                            $('body').append(parsehhmmsst('2:00:00 PM') + '<br>')
                            $('body').append(parsehhmmsst('3:00:00 PM') + '<br>')
                            $('body').append(parsehhmmsst('4:00:00 PM') + '<br>')
                            $('body').append(parsehhmmsst('5:00:00 PM') + '<br>')
                            $('body').append(parsehhmmsst('6:00:00 PM') + '<br>')
                            $('body').append(parsehhmmsst('7:00:00 PM') + '<br>')
                            $('body').append(parsehhmmsst('8:00:00 PM') + '<br>')
                            $('body').append(parsehhmmsst('9:00:00 PM') + '<br>')
                            $('body').append(parsehhmmsst('10:00:00 PM') + '<br>')
                            $('body').append(parsehhmmsst('11:00:00 PM') + '<br>')

                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>








                            share|improve this answer


























                              0












                              0








                              0










                              function parsehhmmsst(arg) {
                              var result = 0, arr = arg.split(':')
                              if (arr[0] < 12) {
                              result = arr[0] * 3600 // hours
                              }
                              result += arr[1] * 60 // minutes
                              result += parseInt(arr[2]) // seconds
                              if (arg.indexOf('P') > -1) { // 8:00 PM > 8:00 AM
                              result += 43200
                              }
                              return result
                              }
                              $('body').append(parsehhmmsst('12:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('1:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('2:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('3:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('4:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('5:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('6:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('7:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('8:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('9:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('10:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('11:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('12:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('1:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('2:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('3:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('4:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('5:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('6:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('7:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('8:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('9:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('10:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('11:00:00 PM') + '<br>')

                              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>








                              share|improve this answer
















                              function parsehhmmsst(arg) {
                              var result = 0, arr = arg.split(':')
                              if (arr[0] < 12) {
                              result = arr[0] * 3600 // hours
                              }
                              result += arr[1] * 60 // minutes
                              result += parseInt(arr[2]) // seconds
                              if (arg.indexOf('P') > -1) { // 8:00 PM > 8:00 AM
                              result += 43200
                              }
                              return result
                              }
                              $('body').append(parsehhmmsst('12:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('1:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('2:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('3:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('4:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('5:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('6:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('7:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('8:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('9:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('10:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('11:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('12:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('1:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('2:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('3:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('4:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('5:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('6:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('7:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('8:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('9:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('10:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('11:00:00 PM') + '<br>')

                              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>








                              function parsehhmmsst(arg) {
                              var result = 0, arr = arg.split(':')
                              if (arr[0] < 12) {
                              result = arr[0] * 3600 // hours
                              }
                              result += arr[1] * 60 // minutes
                              result += parseInt(arr[2]) // seconds
                              if (arg.indexOf('P') > -1) { // 8:00 PM > 8:00 AM
                              result += 43200
                              }
                              return result
                              }
                              $('body').append(parsehhmmsst('12:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('1:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('2:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('3:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('4:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('5:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('6:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('7:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('8:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('9:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('10:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('11:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('12:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('1:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('2:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('3:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('4:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('5:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('6:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('7:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('8:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('9:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('10:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('11:00:00 PM') + '<br>')

                              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>





                              function parsehhmmsst(arg) {
                              var result = 0, arr = arg.split(':')
                              if (arr[0] < 12) {
                              result = arr[0] * 3600 // hours
                              }
                              result += arr[1] * 60 // minutes
                              result += parseInt(arr[2]) // seconds
                              if (arg.indexOf('P') > -1) { // 8:00 PM > 8:00 AM
                              result += 43200
                              }
                              return result
                              }
                              $('body').append(parsehhmmsst('12:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('1:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('2:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('3:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('4:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('5:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('6:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('7:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('8:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('9:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('10:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('11:00:00 AM') + '<br>')
                              $('body').append(parsehhmmsst('12:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('1:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('2:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('3:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('4:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('5:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('6:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('7:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('8:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('9:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('10:00:00 PM') + '<br>')
                              $('body').append(parsehhmmsst('11:00:00 PM') + '<br>')

                              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jul 14 '15 at 17:34









                              Phillip SennPhillip Senn

                              19.4k72213328




                              19.4k72213328























                                  0














                                  You can do this dynamically - in case you encounter not only: HH:mm:ss, but also, mm:ss, or even ss alone.



                                  var str = '12:99:07';
                                  var times = str.split(":");
                                  times.reverse();
                                  var x = times.length, y = 0, z;
                                  for (var i = 0; i < x; i++) {
                                  z = times[i] * Math.pow(60, i);
                                  y += z;
                                  }
                                  console.log(y);





                                  share|improve this answer






























                                    0














                                    You can do this dynamically - in case you encounter not only: HH:mm:ss, but also, mm:ss, or even ss alone.



                                    var str = '12:99:07';
                                    var times = str.split(":");
                                    times.reverse();
                                    var x = times.length, y = 0, z;
                                    for (var i = 0; i < x; i++) {
                                    z = times[i] * Math.pow(60, i);
                                    y += z;
                                    }
                                    console.log(y);





                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      You can do this dynamically - in case you encounter not only: HH:mm:ss, but also, mm:ss, or even ss alone.



                                      var str = '12:99:07';
                                      var times = str.split(":");
                                      times.reverse();
                                      var x = times.length, y = 0, z;
                                      for (var i = 0; i < x; i++) {
                                      z = times[i] * Math.pow(60, i);
                                      y += z;
                                      }
                                      console.log(y);





                                      share|improve this answer















                                      You can do this dynamically - in case you encounter not only: HH:mm:ss, but also, mm:ss, or even ss alone.



                                      var str = '12:99:07';
                                      var times = str.split(":");
                                      times.reverse();
                                      var x = times.length, y = 0, z;
                                      for (var i = 0; i < x; i++) {
                                      z = times[i] * Math.pow(60, i);
                                      y += z;
                                      }
                                      console.log(y);






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Apr 21 '16 at 1:51









                                      Mogsdad

                                      33.3k1290194




                                      33.3k1290194










                                      answered Apr 21 '16 at 0:59









                                      JeffzJeffz

                                      1,27741937




                                      1,27741937






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9640266%2fconvert-hhmmss-string-to-seconds-only-in-javascript%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                                          Calculate evaluation metrics using cross_val_predict sklearn

                                          Insert data from modal to MySQL (multiple modal on website)