Convert HH:MM:SS string to seconds only in javascript
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);
Convert a time format to seconds
Try the below code
var hms = '09:05:03';var s = hms.split(':');
var seconds = (+s[0]) * 60 * 60 + (+s[1]) * 60 + (+s[2]); console.log(seconds);var hm = '00:55:03';var s = hm.split(':');
var seconds = (+s[0]) * 60 * 60 + (+s[1]) * 60 + (+s[2]);
console.log(seconds);
JavaScript seconds to time string with format hh:mm:ss
String.prototype.toHHMMSS = function () {
var sec_num = parseInt(this, 10); // don't forget the second param
var hours = Math.floor(sec_num / 3600);
var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
var seconds = sec_num - (hours * 3600) - (minutes * 60);
if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
return hours+':'+minutes+':'+seconds;
}
You can use it now like:
alert("5678".toHHMMSS());
Working snippet:
String.prototype.toHHMMSS = function () { var sec_num = parseInt(this, 10); // don't forget the second param var hours = Math.floor(sec_num / 3600); var minutes = Math.floor((sec_num - (hours * 3600)) / 60); var seconds = sec_num - (hours * 3600) - (minutes * 60);
if (hours < 10) {hours = "0"+hours;} if (minutes < 10) {minutes = "0"+minutes;} if (seconds < 10) {seconds = "0"+seconds;} return hours + ':' + minutes + ':' + seconds;} console.log("5678".toHHMMSS());
convert time string to seconds in javascript/jquery
I know this will not sound good for some, but if you don't want to use a plugin like moment.js. You could parse it like this (only for format dd'd' hh:mm:ss):
var days = parseInt(time.split('d ')[0]);
var hours = parseInt(time.split('d ')[1].split(":")[0]);
var mins = parseInt(time.split('d ')[1].split(":")[1]);
var secs = parseInt(time.split('d ')[1].split(":")[2]);
hours += days * 24;
mins += hours * 60;
secs += mins * 60;
secs will be the total
Convert HH:MM:SS to seconds in momentjs
Try something like this:
moment('12:10:12: PM', 'HH:mm:ss: A').diff(moment().startOf('day'), 'seconds');
returns 43812
How can I convert '00: 30: 00 ' format to seconds?
Use asSeconds
from momentjs duration objects
moment.duration('00:30:00').asSeconds();
Convert seconds to HH-MM-SS with JavaScript?
Updated (2020):
Please use @Frank's one line solution:
new Date(SECONDS * 1000).toISOString().substring(11, 16)
If SECONDS<3600 and if you want to show only MM:SS then use below code:
new Date(SECONDS * 1000).toISOString().substring(14, 19)
It is by far the best solution.
Old answer:
Use the Moment.js
library.
How to convert a HH:MM:SS string to seconds with PHP?
The quick way:
echo strtotime('01:00:00') - strtotime('TODAY'); // 3600
How can I convert a HH:mm:ss string to a JavaScript Date object?
Try this (without jQuery and a date object (it's only a time)):
var
pieces = "8:19:02".split(':')
hour, minute, second;
if(pieces.length === 3) {
hour = parseInt(pieces[0], 10);
minute = parseInt(pieces[1], 10);
second = parseInt(pieces[2], 10);
}
Related Topics
How to Add an Object to an Array
Sort Mixed Alpha/Numeric Array
Parse JSON String into a Particular Object Prototype in JavaScript
Referencing "This" Inside Setinterval/Settimeout Within Object Prototype Methods
A Cors Post Request Works from Plain JavaScript, But Why Not with Jquery
Vanilla JavaScript Event Delegation
How to Extend Function with Es6 Classes
Why Does Array.Prototype.Push Return the New Length Instead of Something More Useful
How to Duplicate Object Properties in Another Object
Send Message to Specific Client with Socket.Io and Node.Js
How to Call 3 Functions in Order to Execute Them One After the Other
Get Protocol, Domain, and Port from Url
Regular Expression for Password Validation
Coordinating Parallel Execution in Node.Js
Js:Convert Array of Strings to Array of Objects