The following Google Apps Script is designed to check that a date submitted via a Google Form is both in the future and falls on a Wednesday. This was developed as part of a newsletter submission process to help validate information and inform a user when they had not followed the instructions. It has been developed for UK dates and may need adjusting for your own time zone.
Google Form question asking for a date to be entered. |
The Code
The trickiest part of this process is that the data coming from the Form is not recognised as a date specifically. So if you try to create a date object from it immediately you end up with an Americanised version that switches the day/month around. We therefore have to split it apart:
var splitDate = submissionDate.split("/");var day = splitDate[0];var month = splitDate[1];var year = splitDate[2];
Then piece it back together, remembering that JavaScript dates start at '0' for 'January' hence we subtract '1' from the month:
var newSubmissionDate = new Date(year, month - 1, day);
Next we want to look at extracting the day of the week from this date, so we can pop the days into an array and use 'getDay()' to return a value between 0 to 6 in order to work it out:
var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];var dayOfTheWeek = newSubmissionDate.getDay();var day = weekday[dayOfTheWeek];
With a little bit of formatting we can tidy up our submitted date into something like Wednesday 4 December 2024:
var niceSubmissionDate = Utilities.formatDate(newSubmissionDate, timeZone, 'EEEE d MMMMM yyyy');
Which takes us to our final step of comparing dates and checking the day of the week:
if ((newSubmissionDate < todaysDate) && (day != "Wednesday")) {console.log("Form date is in the past and not a Wednesday.");} else if (newSubmissionDate < todaysDate) {console.log("Form date is in the past.");} else if (day != "Wednesday") {console.log("Form date not a Wednesday.");} else {console.log("All is good.");};
Download
Check date in future and a Wednesday folder download (please use 'File' > 'Make a copy' for your own version of each item).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getFormData(e) { | |
// get Spreadsheet TimeZone | |
var timeZone = SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone(); | |
// get Form data | |
var formValues = e.namedValues; | |
// get specific values from Form ****************** | |
var submissionDate = formValues["The Date"][0]; | |
console.log('submissionDate is: ' + submissionDate); | |
// get specific values from Form ****************** | |
// get todays date | |
var todaysDate = new Date(); | |
// fix date format from Form so workable and look nice *********************** | |
var splitDate = submissionDate.split("/"); | |
console.log("splitDate is: " + splitDate); | |
var day = splitDate[0]; | |
var month = splitDate[1]; | |
var year = splitDate[2]; | |
// subtract '1' from month as JavaScript starts at '0' for 'January' | |
var newSubmissionDate = new Date(year, month - 1, day, todaysDate.getHours(), todaysDate.getMinutes()); | |
console.log('newSubmissionDate is: ' + newSubmissionDate); | |
// fix date format from Form so workable and look nice *********************** | |
// create array of days of the week and use 'getDay()' method to determine which one | |
var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; | |
var dayOfTheWeek = newSubmissionDate.getDay(); | |
var day = weekday[dayOfTheWeek]; | |
console.log("Submission day is: " + day); | |
// create a nicely formatted date which could be communicated with an end user | |
var niceSubmissionDate = Utilities.formatDate(newSubmissionDate, timeZone, 'EEEE d MMMMM yyyy'); | |
console.log("niceSubmissionDate is: " + niceSubmissionDate); | |
todaysDate.setHours(0, 0, 0, 0); | |
console.log("todaysDate with hours is: " + todaysDate); | |
if ((newSubmissionDate < todaysDate) && (day != "Wednesday")) { | |
console.log("Form date is in the past and not a Wednesday."); | |
} else if (newSubmissionDate < todaysDate) { | |
console.log("Form date is in the past."); | |
} else if (day != "Wednesday") { | |
console.log("Form date not a Wednesday."); | |
} else { | |
console.log("All is good."); | |
}; | |
}; |
No comments:
Post a Comment