繁体   English   中英

如何使用javascript查找第二天

[英]how to find next day using javascript

我试图找到第二天使用 javascript 它适用于静态日期完美,但不适用于动态日期。

也适用于 1 到 12 之间的日期并为我提供完美的输出,不适用于高日期然后 12

请帮我解决这个问题!

 <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 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;
    };

您可以不带参数调用此函数,它将使用当前日期,日期偏移量为 +1。 或者您可以传入日期并以天为单位进行偏移,它将返回新日期。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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