I don't know anything about JS (will soon be obvious), but I am trying to help a colleague.
This project aids in the calculation of many dates for a paper simulation. The html document must be in one file and work only on the client side. It will not be on a server.
Basically the end-user enters a future reference date into a text-box and clicks "Calculate Dates". At that point 6 dates in the past are calculated based on specific number of days back. Also there may be multiple of the same date embedded throughout the simulation. Eventually part of the content within the body will get printed.
I do a little VBA programming so I tried to translate what I might do in VBA into what I could find on the internet about JS.
Any help or advice is appreciated.
Here is my first try:
You don't know what you don't know...
This project aids in the calculation of many dates for a paper simulation. The html document must be in one file and work only on the client side. It will not be on a server.
Basically the end-user enters a future reference date into a text-box and clicks "Calculate Dates". At that point 6 dates in the past are calculated based on specific number of days back. Also there may be multiple of the same date embedded throughout the simulation. Eventually part of the content within the body will get printed.
I do a little VBA programming so I tried to translate what I might do in VBA into what I could find on the internet about JS.
Any help or advice is appreciated.
Here is my first try:
JavaScript:
<!DOCTYPE HTML>
<html>
<head>
<script>
function CalcDates() {
// gets string from activitydate Textbox and formats into date as variable
var teststring = document.getElementById("activitydate").value; //Entered at Month/Day/Year
var dateParts = testString.split('/');
var referenceDate = new Date(dateParts[1] - 1, dateParts[2], dateParts[0]); //Formatted at Month/Day/Year
//for each element in array sent to function renderNewDate and return that date to
//to be placed in the proper location of body
var a = [0,1,147,148,156,200]; //these are the number of days back from the reference date to be calculated
for (var i=0, tot=a.length; i < tot; i++) {
switch (a) {
case 0: var Date0 = renderNewDate(referenceDate, 0) // pass reference date and days back to 2nd function
document.getElementById('day0a').innerHTML = Date0; // place return result into body of html document
document.getElementById('day0b').innerHTML = Date0; //some dates may appear more than once
break;
case 1: var Date1 = renderNewDate(referenceDate, 1)
document.getElementById('day1').innerHTML = Date1;
break;
case 147: var Date147 = renderNewDate(referenceDate, 147)
document.getElementById('day0').innerHTML = Date147;
break;
case 148: var Date148 = renderNewDate(referenceDate, 148)
document.getElementById('day0').innerHTML = Date148;
break;
case 156: var Date156 = renderNewDate(referenceDate, 156)
document.getElementById('day0').innerHTML = Date156;
break;
case 200: var Date200 = renderNewDate(referenceDate, 200)
document.getElementById('day0').innerHTML = Date200;
break;
}
}
function renderNewDate(referenceDate, days) {
// duplicate the reference date, then subtract the number of days;
// JavaScript will automatically take care of switching to the next month or year if the current one is exceeded
var endDate,
formattedDate;
endDate = new Date(referenceDate.getTime());
endDate.setDate(endDate.getDate() - days);
return = (endDate.getMonth() + 1) + '/' + endDate.getDate() + '/' + endDate.getFullYear();
}
</script>
<body>
<label>Future date of activity (mm/dd/yyyy): </label>
<input type="text" id="activitydate" name="activitydate" />
<br />
<button onclick='doAction();'>Calculate Dates</button>
<br />
<br />
<p id="day0a">0 days back</p>
<p id="day1">1 day back</p>
<p id="day147">147 days back</p>
<p id="day148">148 days back</p>
<p id="day156">156 days back</p>
<p id="day200">200 days back</p>
<p id="day0b">0 days back</p>
</body>
</html>
You don't know what you don't know...