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

  • Home
  • SEARCH
  • 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 8648703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:22:29+00:00 2026-06-12T13:22:29+00:00

I use the following script to calculate date difference between two dates(with time). It

  • 0

I use the following script to calculate date difference between two dates(with time). It works fine for most of the inputs but the problem is it returns wrong output for some inputs. What is wrong with this code

for example when I calculate date difference between

01/10/2012 11AM

and

02/10/2012 12AM

it returns 2 days as the result but its 13hrs.(as special rounding for my application it will be considered as one day) Or is there any alternative method for this. Please comment if any more details are needed.

function dstrToUTC(ds) {
             var dsarr = ds.split("/");
             var dd = parseInt(dsarr[0],10);
             var mm = parseInt(dsarr[1],10);
             var yy = parseInt(dsarr[2],10);
             var hh = parseInt(dsarr[3]); 
             return Date.UTC(yy,mm-1,dd,hh,0,0);
            }
            function datediff() {


             var roomFrom=$("#room_from").val()+"/"+$("#room_time_from").val();
             var foomTo=$("#room_to").val()+"/"+$("#room_time_to").val();
             var d1 = dstrToUTC(roomFrom);
             var d2 = dstrToUTC(foomTo);
             var oneday = 86400000;
             var dayDiff=(d2-d1) / oneday;
             var dayDiff2=Math.floor(dayDiff);


             //used to handle less than one day and, extra hours to a day
             if(dayDiff>dayDiff2)
                 dayDiff=dayDiff2+1; 

             document.getElementById("room_nodays_add").value=dayDiff;

            }
  • 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-12T13:22:30+00:00Added an answer on June 12, 2026 at 1:22 pm

    This should probably not be the answer, but if you want to save time and do clean date computation in javascript, use moment.js

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

Sidebar

Related Questions

The following script works fine: #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; use
I am trying to use setInterval to calculate time left, here is my script:
I use the following script for get the values from HTML Table.If I use
I am trying to use the following script in asp.net: <script language=javascript type=text/javascript> function
Getting an error when i use the following script to show a div when
I use the following bash script to copy only files of certain extension(in this
The following script was recommended to use a List Page to show a Google
The following script is for finding one motif in protein sequence. use strict; use
I have the following script running successfully. However if I try to use a
I have an image uploading script in which i use the following setup to

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.