Sealongmaly
Sealongmaly

Reputation: 51

Subtraction date using JQUERY,AJAX

I have the code as below:

<table>
   <tr>
    <td>Date Start</td>
    <td><input type ="text" name="date" class="validate[required] text-input datepicker TextInput"></td>
   </tr>
   <tr>
    <td>Year Month day</td>
    <td id="showhere">I want to display year,month,day in here by subtraction between date now and date start.</td>
   </tr>
</table>

I need:

I want to subtract date between date now and date start that user input into text box above.I do not know how to fix this.Anyone help me please,Thanks.

Upvotes: 0

Views: 3012

Answers (2)

Kaidul
Kaidul

Reputation: 15895

Firstly, Why you are using text input field to select date? This wouldn't be comfortable for users.Use jquery datepicker instead of it to provide nice user interface and also calculate the date differance after that as following:

See the demo here

$(document).ready(function () { 
$(function() {
        $( "#datepicker" ).datepicker({ dateFormat: 'mm/dd/yy' });
    });
    $('button').click(function() {
        var date = $('#datepicker').datepicker('getDate');
        var today = new Date();
        var diff = Math.floor((today - date) / (1000 * 60 * 60 * 24));
        alert(diff);
});   
});   

Markup:

<head>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.8.2.js"></script>
    <script src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
</head> 
<form>
        <input id="datepicker" type="text" readonly="true"/><br>
    </form>
        <br><button>number of days</button>​

Upvotes: 0

Sibu
Sibu

Reputation: 4617

var d1 = new Date(); 
var d2 = new Date("2011/10/11")  
var diff = Math.abs(d1-d2);  // in milliseconds

But you will have to make sure though that input date format is correct

Upvotes: 1

Related Questions