2016-09-21 68 views
-2

我试图找到第二天使用javascript它为静态日期完美的工作,但没有工作的动态日期。如何找到第二天使用javascript

1日期间的工作还到12,给我完美的输出吧,高日不工作,然后12

请帮我解决这个问题!

<script type="text/javascript"> 
 
function next_day() { \t 
 
\t // var date = document.getElementById('date').value; 
 
\t var textbox_Value = document.getElementById('date').value; 
 
\t console.log("input date : " + textbox_Value); 
 
\t 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 
 
\t var dayWithSlashes = (fDate.getDate()) + '/' + (fDate.getMonth()+1) + '/' + fDate.getFullYear(); 
 
\t console.log("Foramat changed date : " + dayWithSlashes); 
 
\t var day_in_js_format = new Date(dayWithSlashes); 
 
\t console.log("convert in date format : " + day_in_js_format); 
 
\t day_in_js_format.setDate(day_in_js_format.getDate() + 1); 
 
\t console.log("day_in_js_format++ : " + day_in_js_format); 
 
\t var final_Result = (day_in_js_format.getDate()) + '/' + (day_in_js_format.getMonth()+1) + '/' + day_in_js_format.getFullYear(); 
 
\t console.log("next day : " + final_Result); 
 
} 
 
</script>
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <title> 
 
\t \t Next day using javascript 
 
\t </title> 
 
\t <!-- Latest compiled and minified CSS & JS --> 
 
\t <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> 
 
</head> 
 
<body> 
 

 
\t <div class="container fix-top"> 
 
\t \t <form action="" method="POST" role="form"> 
 
\t \t \t <legend>Form title</legend> 
 
\t \t 
 
\t \t \t <div class="form-group"> 
 
\t \t \t \t <label for="">Next day</label> 
 
\t \t \t \t <input type="text" class="form-control" id="date" name="date" placeholder="Input field"> 
 
\t \t \t </div> \t \t \t 
 
\t \t 
 
\t \t \t <button type="button" onclick="next_day();" class="btn btn-primary">Submit</button> 
 
\t \t </form> 
 
\t </div> 
 
\t 
 
\t <script src="//code.jquery.com/jquery.js"></script> 
 
\t <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> 
 
</body> 
 
</html>

+1

症状表明,创建日期时您已将月份和日期混合在一起... – Teemu

+1

构建日期对象时,不要使用非标准的dd/mm/yyyy格式。改为使用yyyy-mm-dd。 – JJJ

+0

我没有混合月份和日期你请检查你的日志以便更好地了解 – HirenMangukiya

回答

0

功能来天添加日期:

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偏移。或者您可以在几天内传入日期和偏移量,并返回新的日期。

+0

谢谢先生,但它不适用于甲酸盐dd/mm/yyyy它的工作为mm/dd/yyyy – HirenMangukiya

+0

您需要提供函数一个有效的javascript日期,该函数返回一个日期对象,这将是正确的,您可以你需要的一切。 – SPlatten