jQuery date calculations (Addition and Subtraction)

Add number of days to current date in jquery

Lets see how to add the required number of days with current date in jquery,

    <script type='text/javascript'>
        $(document).ready(function () {
    var Num_of_days = 2;
    var d = new Date();
    var toDate = new Date();
    toDate.setDate(d.getDate() + Num_of_days);
    alert(toDate);
        });
        </script>
                

This code provides the updated date which is addition of provided number days with today's date as alert message while loading the web page in web browser.

Add number of days to specific date in jquery

Lets see how to add the required number of days with specific date in jquery,

     <script type='text/javascript'>
        $(document).ready(function () {
    var Num_of_days = 2;
    var d = new Date(2019,11,13);
    var toDate = new Date();
    toDate.setDate(d.getDate() + Num_of_days);
    alert(toDate);
        });
                </script>  

This code provides the updated date '2019-11-15' which is addition of provided number days with specific date as alert message while loading the web page in web browser.

Subtract number of days to current date in jquery

Lets see how to subtract the required number of days with current date in jquery,

    <script type='text/javascript'>
        $(document).ready(function () {
    var Num_of_days = 2;
    var d = new Date();
    var toDate = new Date();
    toDate.setDate(d.getDate() - Num_of_days);
    alert(toDate);
        });
             </script>         

This code provides the updated date which is subtraction of provided number days with today's date as alert message while loading the web page in web browser.

Subtract number of days to specific date in jquery

Lets see how to subtract the required number of days with specific date in jquery,

     <script type='text/javascript'>
        $(document).ready(function () {
    var Num_of_days = 2;
    var d = new Date(2019,11,13);
    var toDate = new Date();
    toDate.setDate(d.getDate() + Num_of_days);
    alert(toDate);
        });
                </script>      

This code provides the updated date '2019-11-11' which is subtraction of provided number days with specific date as alert message while loading the web page in web browser.

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^