
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How can I convert 'HH:MM:SS ' format to seconds in JavaScript
We are required to write a function that takes in a ‘HH:MM:SS’ string and returns the number of seconds. For example −
countSeconds(‘12:00:00’) //43200 countSeconds(‘00:30:10’) //1810
Let’s write the code for this. We will split the string, convert the array of strings into an array of numbers and return the appropriate number of seconds.
The full code for this will be −
Example
const timeString = '23:54:43'; const other = '12:30:00'; const withoutSeconds = '10:30'; const countSeconds = (str) => { const [hh = '0', mm = '0', ss = '0'] = (str || '0:0:0').split(':'); const hour = parseInt(hh, 10) || 0; const minute = parseInt(mm, 10) || 0; const second = parseInt(ss, 10) || 0; return (hour*3600) + (minute*60) + (second); }; console.log(countSeconds(timeString)); console.log(countSeconds(other)); console.log(countSeconds(withoutSeconds));
Output
The output in the console will be −
86083 45000 37800
- Related Articles
- How to convert seconds to HH-MM-SS with JavaScript?
- How to Convert Seconds to Time (hh mm ss) or Vice Versa in Excel?
- Display Seconds in ss format (01, 02) in Java
- How to format JavaScript date into yyyy-mm-dd format?
- How to convert time seconds to h:m:s format in Python?
- How to convert JavaScript seconds to minutes and seconds?
- Format hour in HH (00-23) format in Java
- How can I get seconds since epoch in JavaScript?
- Convert MySQL date format from yyyy-mm-ddThh:mm:ss.sssZ to yyyy-mm-dd hh:mm:ss ?
- MySQL date format to convert dd.mm.yy to YYYY-MM-DD?
- How can I convert a bytes array into JSON format in Python?
- In MySQL, how can I convert a number of seconds into TIMESTAMP?\n
- How can I convert a string to boolean in JavaScript?
- Display seconds with SimpleDateFormat('ss') in Java
- Format Minutes in mm format in Java

Advertisements