Convert hours and minute to millisecond using javascript or jQuery

30,275

Solution 1

Try this code:

const tomiliseconds = (hrs,min,sec) => (hrs*60*60+min*60+sec)*1000;

console.log(tomiliseconds(34, 26, 0)); // --> 123960000ms    

Solution 2

This is simple.

var time = "34:26";
var timeParts = time.split(":");
console.log((+timeParts[0] * (60000 * 60)) + (+timeParts[1] * 60000));

Solution 3

Arrow functions + hoisting variation with ES2015:

// Function
const milliseconds = (h, m, s) => ((h*60*60+m*60+s)*1000);

// Usage
const result = milliseconds(24, 36, 0);

// Contextual usage
const time = "34:26";
const timeParts = time.split(":");
const result = milliseconds(timeParts[0], timeParts[1], 0);
console.log(result);

This way you can componetize or make it service

Share:
30,275

Related videos on Youtube

Jitendra Solanki
Author by

Jitendra Solanki

Experienced Software Engineer with a demonstrated history of working in the information technology and services industry. Skilled in PHP, NodeJs, React Js, Vue Js, , Angular 4, Databases, jQuery, and CodeIgniter. Strong engineering professional with a Bachelor of Engineering (B.E.)

Updated on July 05, 2022

Comments

  • Jitendra Solanki
    Jitendra Solanki almost 2 years

    I have Hours:Minute format of time as string. To display it into highchart as time i need to convert this string into milliseconds. For example: 34:26 (34 hours and 26 minutes) millisecond is 124000000 How can i convert it to milliseconds using any of jquery or javascript function.

    • snaplemouton
      snaplemouton over 7 years
      You can simply create a Javascript function that convert it to milliseconds. function(var hours, var minutes, var seconds){ return hours * 3600000 + minutes * 60000 + seconds * 1000; }
    • Naghaveer R
      Naghaveer R over 7 years
  • Esger
    Esger almost 4 years
    Shorter using the spread operator ... : const milliseconds = (h, m, s = 0) => (h*60*60+m*60+s)*1000; const time = "34:26"; const result = milliseconds(...time.split(':'));