简体   繁体   中英

how to find next day using javascript

I was tried to find next day using javascript it working for static date perfect, but not worked for dynamic date.

Also work for date between 1 to 12 and give me perfect output for it, not work for high date then 12

Please help me to solve this problem!

 <script type="text/javascript"> function next_day() { // var date = document.getElementById('date').value; var textbox_Value = document.getElementById('date').value; console.log("input date : " + textbox_Value); var fDate = new Date(textbox_Value.replace( /(\\d{2})-(\\d{2})-(\\d{4})/, "$2/$1/$3") ); //change date formate mm/dd/yyyy to dd/mm/yyyy var dayWithSlashes = (fDate.getDate()) + '/' + (fDate.getMonth()+1) + '/' + fDate.getFullYear(); console.log("Foramat changed date : " + dayWithSlashes); var day_in_js_format = new Date(dayWithSlashes); console.log("convert in date format : " + day_in_js_format); day_in_js_format.setDate(day_in_js_format.getDate() + 1); console.log("day_in_js_format++ : " + day_in_js_format); var final_Result = (day_in_js_format.getDate()) + '/' + (day_in_js_format.getMonth()+1) + '/' + day_in_js_format.getFullYear(); console.log("next day : " + final_Result); } </script>
 <!DOCTYPE html> <html> <head> <title> Next day using javascript </title> <!-- Latest compiled and minified CSS & JS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> </head> <body> <div class="container fix-top"> <form action="" method="POST" role="form"> <legend>Form title</legend> <div class="form-group"> <label for="">Next day</label> <input type="text" class="form-control" id="date" name="date" placeholder="Input field"> </div> <button type="button" onclick="next_day();" class="btn btn-primary">Submit</button> </form> </div> <script src="//code.jquery.com/jquery.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> </body> </html>

Function to add days to a date:

    function addDays(dtRef, intDays) {
        if ( !(typeof dtRef == "object"
            && typeof dtRef['setDate'] == "function"
            && typeof dtRef['getDate'] == "function") ) {
            dtRef = new Date();
        }
        if ( typeof intDays != "number" ) {
            intDays = 1;
        }
        dtRef.setDate(dtRef.getDate() + intDays);
        return dtRef;
    };

You can call this function with no parameters and it will use the current date with a day offset of +1. Or you can pass in the date and offset in days and it will return the new date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM