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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:53:48+00:00 2026-06-17T05:53:48+00:00

I am trying to calculate the time between two times on the current date

  • 0

I am trying to calculate the time between two times on the current date using JavaScript. There are other questions similar to this one, but none seem to work, and few with many upvotes that I can find.

I have the following, which fails on the line: var diff = new Date(time1 - time2);, which always gives me an invalid Date when alerted, so it is clearly failing. I cannot work out why.

The initial date is added in the format of: hh:mm:ss in an input field. I am using jQuery.

$(function(){

$('#goTime').click(function(){
  var currentDate = new Date();

  var dateString = (strpad(currentDate.getDate()) +'-'+ strpad(currentDate.getMonth()+1)+'-'+currentDate.getFullYear()+' '+ $('#starttime').val());

  var time1 = new Date(dateString).getTime();
  var time2 = new Date().getTime();

  var diff = new Date(time1 - time2);

  var hours = diff.getHours();
  var minutes = diff.getMinutes();
  var seconds = diff.getMinutes();

  alert(hours + ':' + minutes + ':' + seconds);

});
  });
  function strpad(val){ 
        return (!isNaN(val) && val.toString().length==1)?"0"+val:val; 
  }

dateString is equal to: 14-01-2013 23: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-06-17T05:53:49+00:00Added an answer on June 17, 2026 at 5:53 am

    You have the fields in dateString backwards. Swap the year and day fields…

    > new Date('14-01-2013 23:00')
    Invalid Date
    > new Date('2013-01-14 23:00')
    Mon Jan 14 2013 23:00:00 GMT-0800 (PST)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to calculate the difference between two times using JavaScript. It's just
I am trying to calculate difference(in seconds) between two date/times formatted as following: 2010-05-11
Friends,I am trying to calculate the difference between two date&time entries that the user
I'm trying to calculate elapsed hours between two times. When calculating PM to AM
So, I am trying to calculate the time between two dates that fits certain
I am trying to calculate distance between two corelocation objects like this: float meters
Trying to compare a given Time between two times to see if it's within
I am trying to calculate the difference between given two times and i find
I'm trying to calculate time blocks from given date range. Here's my attempt: <?php
I'm trying to calculate the number of days between two days, but I'm running

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.