0

I am trying to get a date from an html input to javascript, substract 3 years from it and then place the new date back into another html date input field.

I got the substraction to work with the current date but I can not get it to work when I fetch a date from an html input.

Below my code that works:

<input id="InputDate" data-role="datepicker"/>
<input id="OutputDate" data-role="datepicker"/>

<script type="text/javascript">
    var dt = new Date(); // this line I want to replace with the next 2 lines!
    //var inDate= document.getElementById("InputDate").value;
    //var dt = new Date(inDate);
    dt.setFullYear(dt.getFullYear() - 3);
    var datestring = ("0" + dt.getDate()).slice(-2) + "." + ("0"+(dt.getMonth()+1)).slice(-2) + "." +  dt.getFullYear();
    document.getElementById("OutputDate").value = datestring;

When I fetch a date from the input field I get 'aN.aN.NaN' back into the output field. I have tried document.getElementById("InputDate").valueAsDate instead, but this did not work either. When I place an alert(dt) after the instantiation of dt I get 'Invalid Date'.

Any suggenstions how to get the fetched date in the DateObject correctly?

Regards, Manu

2
  • What valid/safe date formats to let parse by the Date constructor are, you can research. If your input date is in a different format, then you will have to modify it first. Commented Jun 19, 2017 at 10:01
  • What date format InputDate field have? Commented Jun 19, 2017 at 10:16

5 Answers 5

2

Just need to pass correct format to date() function:

    //var dt = new Date(); // this line I want to replace with the next 2 lines!
    var inDate= document.getElementById("InputDate").value;
    inDate = inDate.split(".");
    var dt = new Date(inDate[2],inDate[1],inDate[0]);
    //alert(dt);
    dt.setFullYear(dt.getFullYear() - 3);
    
    var datestring = ("0" + dt.getDate()).slice(-2) + "." + ("0"+(dt.getMonth()+1)).slice(-2) + "." +         dt.getFullYear();
    document.getElementById("OutputDate").value = datestring;
<input id="InputDate" data-role="datepicker" value="19.05.2015"/>
<input id="OutputDate" data-role="datepicker"/>

Sign up to request clarification or add additional context in comments.

Comments

1
var inputValues = document.getElementById("InputDate").value.split(".");
var dt = new Date(inputValues[2], inputValues[1] - 1, inputValues[0]);

If you get something like 22.12.1985 as InputDate value

Comments

1

first verify your ,document.getElementById("InputDate").value; is returning correct date

    var inDate= document.getElementById("InputDate").value;
    var dt = new Date(Date.parse(inDate));  //use parse input to corret date format
    dt.setFullYear(dt.getFullYear() - 3);

1 Comment

The concept of Date.parse is nice but unfortunately it is not a recommended practice; see developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
1

Use date format

var date = "2017-06-19";//yyyy-mm-dd or mm-dd-yyyy
var dt = new Date(date);
console.log(dt);
//output Mon Jun 19 2017 00:00:00 GMT+0545 (Nepal Standard Time)
// if you want to set the date to new output text box 
var newDate = dt.getFullYear() + "-" + (dt.getMonth() + 1) + "-" + dt.getDate();
console.log(newDate);

Comments

1

var d = document;
d.g = d.getElementById;

var inDate = d.g("InputDate");
var outDate = d.g("OutputDate");
var datestring = "";
var mo = "";
var dt = null;


inDate.onchange = function() {
  dt = new Date(inDate.value);
  dt.setFullYear(dt.getFullYear() - 3);
  datestring = ("0" + (dt.getMonth() + 1)).slice(-2) + "." + ("0" + dt.getDate()).slice(-2) + "." + dt.getFullYear();
  outDate.value = datestring;

};
<input id="InputDate" data-role="datepicker" value="mm.dd.yyyy"><input id="OutputDate" data-role="datepicker" />

This revision makes use of the onchange event attribute which avoids the "NaN" message displaying in response to the default value of "mm.dd.yyyy" which serves as a prompt for the user. It also uses some short-cuts to reduce the verbosity of the code. Note: while the official example elegantly splits on a "." to get the date values, taking the time to use the Date methods to extract the month, day and year has an advantage despite the extra code. Splitting on a "." works as long as the input data contains a period to demarcate the month, day and year values. But the user could instead use a "/" or a "-" and then that splitting code would not yield the correct result, unless there was code that checked for the period.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.