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 6118353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:25:36+00:00 2026-05-23T15:25:36+00:00

I have two time strings in hh:mm:ss format. (eg: 12:40:13 and 20:01:01 .) How

  • 0

I have two time strings in hh:mm:ss format. (eg: 12:40:13 and 20:01:01.) How can I compare these in JavaScript?

  • 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-23T15:25:36+00:00Added an answer on May 23, 2026 at 3:25 pm

    DEMO HERE

    I prefer to have date objects, but as pointed out elsewhere, you can just convert to seconds if you want to do simple compares

    function dateCompare(time1,time2) {
      var t1 = new Date();
      var parts = time1.split(":");
      t1.setHours(parts[0],parts[1],parts[2],0);
      var t2 = new Date();
      parts = time2.split(":");
      t2.setHours(parts[0],parts[1],parts[2],0);
    
      // returns 1 if greater, -1 if less and 0 if the same
      if (t1.getTime()>t2.getTime()) return 1;
      if (t1.getTime()<t2.getTime()) return -1;
      return 0;
    }
    
    alert(dateCompare("12:40:13","20:01:01"));
    

    For seconds:

    function dateDiff(time1,time2) {
      var t1 = new Date();
      var parts = time1.split(":");
      t1.setHours(parts[0],parts[1],parts[2],0);
      var t2 = new Date();
      parts = time2.split(":");
      t2.setHours(parts[0],parts[1],parts[2],0);
    
      return parseInt(Math.abs(t1.getTime()-t2.getTime())/1000);
    }
    

    Assuming you have 24 hour times and same padding you can do simple string compare

     var t1 = "12:40:13", t2= "20:01:01";
     if (t1<t2) {
        console.log(t1," is < ", t2);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two time strings in HH:MM:SS format. For example, str1 contains 10:20:45 ,
I have two strings which are used to store time in the format hh:mm
I have two arrays with time values in them in this format. 00:00:00 which
Greetings, I have file with the following strings: string.Format({0},{1}, Having \Two\ On The Same
I have a text file that contains date, and two time-strings separated by spaces
I have two variables: char charTime[] = TIME; char buf[] = SOMETHINGELSE; I want
i have two files:(localhost/template/) index.php template.php each time when i create an article(an article
I have two tables, both with start time and end time fields. I need
I have two tables, news and news_views. Every time an article is viewed, the
I have two tables like of this structure: content (content_id, content_type, user_id, time, comment_count)

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.