Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i need to subtract datetimepicker value form current date, separately.

start date is my picker id.

What I have tried:

JavaScript
function getdate() {
        

        var tt = document.getElementById('Startdate').val();
        alert(tt);

        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth() + 1;
        var yyyy = today.getFullYear();
        tod = mm + '/' + dd + '/' + yyyy;
      
        
      
    }
JavaScript

Posted
Updated 26-Oct-16 1:35am

So, the JS Date object is basically just the number of milliseconds from the Unix epoch. You can directly get this using dateObject.getTime().

Therefore, for subtracting a datepicker date from the current date:
JavaScript
var tt = document.getElementById('Startdate').val();
var diff = Date.now() - new Date(tt).getTime();
var seconds = diff/1000;
var minutes = diff/60000;
var hours = diff/3600000;
var days = diff/86400000;


MDN is a good resource for JavaScript documentation:
Date - JavaScript | MDN[^]
 
Share this answer
 
v2
Comments
T_Sub 26-Oct-16 7:43am    
thanks but got error ,tt.getTime is not a function
Nathan Minier 26-Oct-16 7:46am    
Allright, updated. I sort of assumed you or your framework was already converting the tt variable from the datepicker. My bad.
Afzaal Ahmad Zeeshan 27-Oct-16 7:21am    
5ed.
HI All, Thanks for your helps, i have solved this. [Get duration as days, calculations between Current date and date you selected]

JavaScript
<script type="text/javascript">
    $(document).ready(function () {
      

        $("#enddate").datepicker({
            changeMonth: true,
            changeYear: true,
            firstDay: 1,
            dateFormat: 'dd/mm/yy',
        }).datepicker("setDate", "0");

        $("#Startdate").datepicker({
            changeMonth: true,
            changeYear: true,
            firstDay: 1,
            dateFormat: 'dd/mm/yy',
        })

        $("#Startdate").datepicker({ dateFormat: 'dd-mm-yy' });
        $("#enddate").datepicker({ dateFormat: 'dd-mm-yy' });

        $('#notificationstatus').on('change', function () {

            if ($(this).val() == 10) {
                var start = $('#Startdate').datepicker('getDate');
                var end = $('#enddate').datepicker('getDate');

                if (start < end) {
                    var days = (end - start) / 1000 / 60 / 60 / 24;
                    $('#days').text(days);
                }
                else {
                    $('#days').text("Time Mismatched !");
                }
            }
            else if ($(this).val() == 20) {
                var start = $('#Startdate').datepicker('getDate');
                var end = $('#enddate').datepicker('getDate');

                if (start < end) {
                    var days = (end - start) / 1000 / 60 / 60 / 24;
                    $('#days').text(days);
                }
                else {
                    $('#days').text("Time Mismatched !");
                }

            }
        });
    });



Thank You !!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900