I have two fields start date and end date. I am trying to add one year to the what ever the date selected for start date. Here is my view
@Html.TextBoxFor(m => m.StartDate, new {id="StartDateId", style="width: 113px;", onblur = "dateformat(this,'MM/DD/YYYY')"})
<img src="~/Images/calendar.jpg" onclick="scwShow(StartDateId,event,'MM/DD/YYYY');return false;" width="17" height="16"/>
@Html.TextBoxFor(m => m.EndDate, new {id="EndDateId", style="width: 113px;", onblur = "dateformat(this,'MM-DD-YYYY')" })
<img src="~/Images/calendar.jpg" onclick="scwShow(EndDateId,event,'MM-DD-YYYY');return false;" width="17" height="16"/>
I have written java script like this
$('#StartDateId').focusout(function (Cal) {
var myDate = new Date(document.getElementById('StartDateId').value);
var datetime = new Date(date+' '+time);
// format the output
var month = myDate.getMonth() + 1;
var day = myDate.getDate() - 1;
var year = myDate.getFullYear() + 1;
var dateTimeString = month + '/' + day + '/' + year;
alert(dateTimeString);
$('#EndDateId').val == "";
$('#EndDateId').val(dateTimeString);
myDate = myDate.setMonth(myDate.getMonth() + 12);
alert(myDate);
$("#EndDateId").val(myDate);
});
Please help me how to format the time selected. How can I proceed fuerther
For adding or subtracting month,year w3schools provided the setFullYear() function. You can directly add or subtract whatever date you want
See example:
<html>
<body>
<p>Click the button to display the date 1 year ahead of the given date.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var d = new Date();
d.setFullYear(d.getFullYear(), d.getMonth() +12);
document.getElementById("demo").innerHTML = d;
}
</script>
</body>
</html>
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.