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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:23:28+00:00 2026-05-27T13:23:28+00:00

this is my code: $(function() { $( #arr_date ).datepicker({ dateFormat: ‘dd-mm-yy’ }); $( #dep_date

  • 0

this is my code:

$(function() {
    $( "#arr_date" ).datepicker({ dateFormat: 'dd-mm-yy' });
    $( "#dep_date" ).datepicker({ dateFormat: 'dd-mm-yy' });
});

$(function() {
    var start = $('#arr_date').val();
    var end = $('#dep_date').val();
    var diff = new Date(end - start);
    var days = diff/1000/60/60/24;
    $('#num_nights').val(days);  
});

I found this date difference solution here on stackoverflow but it doesn’t work for me.

It does not calculate the date differences at all. I thought it might be because of the date formatting (dd-mm-yy). I’ve tried it with yyyy-mm-dd formatting too but it did not work either.

Could anyone help me and tell me what might cause the problem? Thank you.

  • 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-27T13:23:29+00:00Added an answer on May 27, 2026 at 1:23 pm

    You don’t want to use .val() here, that will give you a string and subtracting strings from each other isn’t terribly useful. The datepicker has a method, getDate, that gives you a Date object so you could use that:

    var start = $('#arr_date').datepicker('getDate');
    var end   = $('#dep_date').datepicker('getDate');
    var days   = (end - start)/1000/60/60/24;
    

    Demo: http://jsfiddle.net/ambiguous/TCXcX/

    Also, this code:

    $(function() {
        var start = $('#arr_date').val();
        var end = $('#dep_date').val();
        var diff = new Date(end - start);
        var days = diff/1000/60/60/24;
        $('#num_nights').val(days);  
    });
    

    will run when the DOM is ready but you don’t want it to run until the dates have been selected. You’d need to bind your (corrected) calculation code to a button or watch the onSelect events:

    function showDays() {
        var start = $('#arr_date').datepicker('getDate');
        var end   = $('#dep_date').datepicker('getDate');
        if(!start || !end)
            return;
        var days = (end - start)/1000/60/60/24;
        $('#num_nights').val(days);  
    }
    
    $( "#arr_date" ).datepicker({ dateFormat: 'dd-mm-yy', onSelect: showDays });
    $( "#dep_date" ).datepicker({ dateFormat: 'dd-mm-yy', onSelect: showDays });
    

    And another demo: http://jsfiddle.net/ambiguous/5BbGS/

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

Sidebar

Related Questions

I have this code, $(function () { $.post(fetch_data.php?url=+$('#url').val(), { }, function(response){ var arrValues =
View this code: function testprecision(){ var isNotNumber = parseFloat('1.3').toPrecision(6); alert(typeof isNotNumber); //=> string }
Hello I get a weird error in this code: function validateForm(aform) { var s=;
I have this piece of code: function func1(text) { var pattern = /([\s\S]*?)(\<\?(?:attrib |if
I have this code: var ar = [10,7,8,3,4,7,6]; function isin(n,a){ for (var i=0;i<a.length;i++){ if
This code function LoadContent(Id) { alert('Controls/Network/NetworkDetail.aspx?' + rnd() + '&CtlId=' + Id); $.get('Controls/Network/NetworkDetail.aspx?' +
With this code function someFunction(classParam:Class):Boolean { // how to know if classParam implements some
I have this code: function submitTwice(f){ f.action = 'http://mydomain.com/threevideopage.php'; f.target='name'; f.submit(); } I left
I have just come across this code: function test(){ //... if ( $profilerule ==
i have this code $(function() { $('#ans_vote a span').click(function(){alert('working');return false;});}); and this html <div

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.