I have Hours:Minute
format of time as string
type. To display them in highchart as a time I need to convert this string
into milliseconds.
For example: 34:26
(34 hours and 26 minutes) milliseconds are 124000000
How can i convert it to milliseconds using any of jquery
or javascript
function?
I have Hours:Minute
format of time as string
type. To display them in highchart as a time I need to convert this string
into milliseconds.
For example: 34:26
(34 hours and 26 minutes) milliseconds are 124000000
How can i convert it to milliseconds using any of jquery
or javascript
function?
- 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; } – snaplemouton Commented Dec 23, 2016 at 7:26
- Have a look at MDN developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… – Naghaveer R Commented Dec 23, 2016 at 7:28
3 Answers
Reset to default 19Try this code:
const toMilliseconds = (hrs,min,sec) => (hrs*60*60+min*60+sec)*1000;
console.log(toMilliseconds(34, 26, 0)); // --> 123960000ms
This is simple.
var time = "34:26";
var timeParts = time.split(":");
console.log((+timeParts[0] * (60000 * 60)) + (+timeParts[1] * 60000));
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