0

I am using the following code to display the date in a form.

In the form I have a field titled "Duration" being weekly rental eg 3 weeks, 5 weeks etc..

I would like the "Return Date" do display the date "X" number of weeks from the date the form is filled out, what ever "Duration" was selected. How can I (+ 7) the Date but instead of (+ 7) do a (+ Duration)?


ALSO I don't know how to return this "Date" to my Excel spread sheet (Google Sheets) All the other fields work fine but I don't know what I am missing or how to return the inline JavaScript result to the excel/google sheets.


<span id="time" name="time" class="form-control" readonly>

<script type="text/javascript">
var d=new Date();
d.setDate(d.getDate() + 7);

var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday",
                "Friday","Saturday")
var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug",
                "Sep","Oct","Nov","Dec")
document.write(weekday[d.getDay()] + " ")
document.write(d.getDate() + " ")
document.write(monthname[d.getMonth()] + " ")
document.write(d.getFullYear() + " ")


</script>
</span>

____________________________________________________________________________

If you need the whole code I would be happy to share it but have only included the string which I am trying to reflect the current date + Duration in a readonly form field.

Thanks in advance if I don't reply soon. Jeff

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154

1 Answers1

-1

You can get the next date with duration with this suppose

currentDate = new Date();

and

duration = 3

then the next date

const nextdate = new Date(currentDate.getTime() + duration*7*24*60*60*1000);

console.log(nextdate .toLocaleDateString()); 

you can format the date aas string like this

const nextDateString= ("0" + resultDate.getDate()).slice(-2) + "/" +("0" + (resultDate.getMonth() + 1)).slice(-2) + "/" + resultDate.getFullYear();
console.log(nextDateString);

You can bind the event listner, by which input you are using for duration selection and there you can set this date to value of your readonly input

document.getElementById("time").value = nextDate;
Nizal Sha
  • 94
  • 9
  • Adding days as multiples of 8.64e ms is not a good idea as in places where daylight saving is observed, not all days are 24 hours long. See [*How to add days to Date?*](https://stackoverflow.com/questions/563406/how-to-add-days-to-date) – RobG May 01 '23 at 20:18
  • Thanks for the reply, I will try it soon. It isn't Urgent. I appreciate your time. Thank you very much. – Jeff Ryad Arlan Cal May 04 '23 at 10:59