-4

How can I display the date to exactly after 2 years depending on the user entered date.by the way i am using "input type=date" method.Can you just provide me ideas of displaying the exact 2 years after date

example: if a users enters the date in the text box using the calendar I need the date to be exactly 2 years after to be displayed in the other text box

example 1: if I enter date in the text box (lets just take its as some product manufacture date), I want it to automatically calculate the the entered date and display the 2 years later on date(the expiry date)

by the I am using javascript.I am happy if I get any ideas based on html, jQuery

Jaywant Khedkar
  • 5,941
  • 2
  • 44
  • 55
usr
  • 1
  • 5
  • 2
    You’re expected to do your own research, before you ask here. What have you done in that regard? Even a simple Google query such as [javascript add years to given date](https://www.google.com/search?q=javascript+add+years+to+given+date) should have given you several results to work with already. – CBroe Sep 25 '17 at 11:47
  • https://stackoverflow.com/questions/10931288/how-to-add-subtract-dates-with-javascript . – mns Sep 25 '17 at 11:53
  • Now you've told us what you need, please show also what've you done so far. – Teemu Sep 25 '17 at 12:12
  • If the answer provided is correct please mark it as correct – Suvethan Nantha Sep 26 '17 at 07:59

1 Answers1

1

First of all add the below scripts to html

<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
</head>
<body>
    <input id="dtePicker" type="date"/>
    <text id="displayDate"></text>
</body>
</html>

Thereafter, add the following script in your js

$('#dtePicker').change(function(val){
  var val=$('#dtePicker').val();
  var add=moment(val).add(1, 'y').toDate();
  add=moment(add).format("MM/DD/YYYY");
  $('#displayDate').text(add);
});

I hope this will help you.

Working Example

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
Suvethan Nantha
  • 2,404
  • 16
  • 28