如何使用javascript找到第二天

时间:2016-09-21 07:50:17

标签: javascript html

我试图在第二天使用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>
&#13;
<!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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分: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。或者您可以在几天内传递日期和偏移量,它将返回新日期。