2

I only have a basic understanding of JavaScript, and I have spent a long time researching and trying to find a solution, I have seen some that came close on her, but they either dealt with a different issue or I was unable to implement the solution with the information provided.

THE PROBLEM;

I have a form that a user can pick a date from, and a submit button. I want to collect the submitted date and then populate several "fields" with new dates which are - 180 days, -165 days, -150 days etc.

So far I have managed to collect the input date and been able to place that date into the fields , but I can't get the subtract x number of days to work.

Script is below and live preview is here (date picker only seems to work on chrome) http://jsfiddle.net/RobLondon/fnsa22r2/1/

I would be grateful for any help you could provide.

Thanks!

<script type="text/javascript">

"use strict";

document.getElementById('inc-date').addEventListener('submit', calcdates);

function calcdates(event) {

event.preventDefault(); 

var incDate = document.getElementById('inc-date-selected').value;

var incdateresult = (incDate);

document.getElementById("180-days").innerHTML = incdateresult - 180;
document.getElementById("165-days").innerHTML = incdateresult - 165;

}

rjh1000
  • 23
  • 2

2 Answers2

0

You could do something like this.

var d = new Date(document.getElementById('inc-date-selected').value);

document.getElementById("180-days").innerHTML = (new Date().setDate(d.getDate() - 180)).toString();
document.getElementById("165-days").innerHTML = (new Date().setDate(d.getDate() - 165)).toString();
lhoworko
  • 1,191
  • 2
  • 13
  • 24
  • Hi SailorLeroy, Thanks or getting back to me on this, you solution certainly does something , but I get a long string of numbers in each field. Is that the date as a 'string'? If so how do I get it to display as a date formatted as the input date i.e 2014-08-23 Cheers – rjh1000 Aug 14 '14 at 07:19
  • Ah yes, sorry. That is called the UNIX timestamp -- the number of seconds since Jan 1 1970 UTC. Rather than toString() at the end, replace that with toLocaleDateString(). – lhoworko Aug 14 '14 at 15:24
  • Hi, that seems to stop it from performing the action as before... [link](http://jsfiddle.net/RobLondon/fnsa22r2/5/) – rjh1000 Aug 14 '14 at 15:42
  • Let me take a further look. Ill get back to you in a few minutes – lhoworko Aug 14 '14 at 15:42
  • Ok here we go. replace the Date stuff with this. document.getElementById("180-days").innerHTML = new Date(Date.parse(d) - (180 * 86400 * 1000)).toDateString(); and document.getElementById("165-days").innerHTML = new Date(Date.parse(d) - (165 * 86400 * 1000)).toDateString(); – lhoworko Aug 14 '14 at 15:56
  • If you want it in a format closer to how you specified (YYYY-MM-DD) try .toLocaleDateString() at the end. Whatever is your preference. – lhoworko Aug 14 '14 at 15:58
  • That is Bang on mate, thank so much for sorting that one out! – rjh1000 Aug 15 '14 at 08:10
0

I think you want something like this (just replace the new Date with new Date(fields date):

var myDate = new Date("1/1/1990");
var dayOfMonth = myDate.getDate();
myDate.setDate(dayOfMonth - 1);

It's taken from http://msdn.microsoft.com/en-us/library/ie/ee532932(v=vs.94).aspx if you would like more info.

Blue
  • 463
  • 4
  • 18