Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6786445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:14:46+00:00 2026-05-26T17:14:46+00:00

I want to convert time data to the format HH:mm:ss in JavaScript. I’ve got

  • 0

I want to convert time data to the format HH:mm:ss in JavaScript.
I’ve got a problem in my code (see comments inside the code):

function parseTime(timeString){

    var timeString = timeString.toLowerCase();
    timeString = $.trim(timeString);

    var regEx = /^([0-9]|1[0-9]|2[0-3])$/;
    var regEx2 = /^([0-9]|1[0-9]|2[0-3])\.?([0-5][0-9])$/;
    var regEx3 = /^([0-9]|1[0-2])(a|p|am|pm)$/;
    var regEx4 = /^([1-9]|10|11|12)\.?([0-5][0-9])(a|p|am|pm)$/;

    if(regEx.test(timeString)){
        var hours = timeString;
        if(hours.length == 1){
            hours = '0' + hours;
        }
        return hours + ':00:00';
    }
    else if(regEx2.test(timeString)){
        var hoursEndIndex, minutesStartIndex;
        if(timeString.indexOf('.')){
            hoursEndIndex = timeString.indexOf('.');
            minutesStartIndex = timeString.indexOf('.') + 1;
        }else if(timeString.length == 3){//Problem here timeString.length returns 3 but the code below isn't executed?
            hoursEndIndex = 1;
            minutesStartIndex = 1;
        }else if(timeString.length == 4){//Same thing here?
            hoursEndIndex = 2;
            minutesStartIndex = 2;
            return timeString.length;
        }
        var hours = timeString.substring(0, hoursEndIndex);
        if(hours.length == 1){
            hours = '0' + hours;
        }
        var minutes = timeString.substr(minutesStartIndex, 2);
        return hours + ':' + minutes + ':00';
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:14:46+00:00Added an answer on May 26, 2026 at 5:14 pm

    I think you are using indexOf incorrectly here:

    if(timeString.indexOf('.')){
    

    From the documentation:

    Returns the first index at which a given element can be found in the array, or -1 if it is not present.

    Probably you mean this:

    if(timeString.indexOf('.') > -1) {
    

    With your code the expression in the first if statement will be true even if the string does not contain a dot. This means that the else if statement will never be executed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert GMT +530 to CET (Central European Time) in iPhone sdk?
What I want to do is convert an epoch time (seconds since midnight 1/1/1970)
I want to convert a number that is in PRTime format (a 64-bit integer
I want to convert a std::string to lowercase. I am aware of the function
I want to convert the following json and put the values into a data
Ok, so I want to convert a date value to properly format my date
My single-page javascript app retrieves data in JSON format via REST calls. Dates come
I am reading in log data with the following time stamp format: Sat Aug
Specifically, I want to convert the XML structure into this format for the database
I'm creating a script to convert a whole lot of data into CSV format.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.