a month ago

How can I use JavaScript to add weeks to the current date and display the result in an HTML form field?

I have implemented the following code to display the date in a form. In the form, there is a field called "Duration" which represents weekly rental periods (e.g. 3 weeks, 5 weeks, etc.). I want the "Return Date" to show the date that is "Duration" number of weeks from the date the form is filled out. How can I modify the code to add "Duration" instead of just adding 7 days? Also, I am facing difficulty returning the "Date" to my Google Sheets file. All other fields work perfectly, but I don't know what I am missing or how to return the inline JavaScript result to the spreadsheet. Here's the code snippet that I have used to display the date:
<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",
var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug",
document.write(weekday[d.getDay()] + " ")
document.write(d.getDate() + " ")
document.write(monthname[d.getMonth()] + " ")
document.write(d.getFullYear() + " ")
Let me know if you require the complete code. I have only included a string in the code that displays the current date + Duration in a readonly form field. Thank you in advance for your help. Regards, Jeff
a month ago
Verified Answer
To add weeks to the date in JavaScript, you can use the setDate() method. In your code snippet, replace the line d.setDate(d.getDate() + 7); with d.setDate(d.getDate() + (7 * Duration)); where Duration is the number of weeks you want to add. This will add the number of weeks specified in the "Duration" field to the current date. Regarding your second question, you can use Google Apps Script to return the result of your inline JavaScript code to your Google Sheets file. You can use the getRange() method to get a reference to the cell where you want to write the result and then use the setValue() method to write the value. Here's an example:
function writeToSheet() {
  var d = new Date();
  d.setDate(d.getDate() + (7 * Duration));
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getRange("A1");
This function will write the result of your code snippet to cell A1 in your active sheet. You can then call this function from a button or menu item in your Google Sheets file. I hope this helps! Let me know if you have any other questions.


Add Week(s) to a Date in JavaScript or Node.jsHow to Add Weeks to a Date in JavaScript - Coding BeautyHow to Add Weeks to a Date Using JavaScript? - The Web DevHow to add weeks to date using javascript? - Stack Overflowjavascript - How to add days to Date? - Stack Overflow
a month ago
To add the selected duration to the current date to get the return date, you can retrieve the selected value of the "Duration" field and convert it to a number. Then, you can add that number of weeks to the current date using the setDate() method of the Date object. Here is an example code:
var durationField = document.getElementById("duration");
var durationInWeeks = parseInt(durationField.value); // Assumes the value is a number
var returnDate = new Date();
returnDate.setDate(returnDate.getDate() + (durationInWeeks * 7)); // Add weeks
To show the return date on the page, you can create another element (e.g. a ) and set its innerHTML to the formatted date. For example:
<span id="returnDate"></span>
var returnDateElement = document.getElementById("returnDate");
returnDateElement.innerHTML = weekday[returnDate.getDay()] + " " + returnDate.getDate() + " " +
                               monthname[returnDate.getMonth()] + " " + returnDate.getFullYear();
As for returning the result to Google Sheets, you can write a Google Apps Script function that retrieves the value of the form field and calculates the return date based on it, and then sets the value of another cell to the formatted date. You can then use this function in a cell formula to show the return date. Here is an example Apps Script function:
function calculateReturnDate(durationInWeeks) {
  var returnDate = new Date();
  returnDate.setDate(returnDate.getDate() + (durationInWeeks * 7));
  var formattedDate = weekday[returnDate.getDay()] + " " + returnDate.getDate() + " " +
                      monthname[returnDate.getMonth()] + " " + returnDate.getFullYear();
  return formattedDate;
To use this function in a Google Sheets cell, you can enter the following formula in the cell:
where A1 is the cell that contains the duration in weeks. Note that you will need to enable the Google Sheets API and create a project and a script file to use Apps Script functions.