How to Use javascript Function for Default Date Value
Followed by 3 people
Is it possible to use javascript to set the default input value in an input element?
In my case, I have a report that needs to default a starting date to the most recent July 1st (this year if current date is after July 1, last year if not).
script:
function getMostRecentJuly1st() {
const today = new Date();
const currentYear = today.getFullYear();
// Create a date for July 1st of the current year
const july1st = new Date(currentYear, 6, 1); // Month is 6 because January is 0
// If today's date is before July 1st, use last year's July 1st
if (today < july1st) {
return new Date(currentYear - 1, 6, 1);
}
return july1st;
}
0
-
Worked out solution:
1. Added a JavaScript file to the application called "CustomDateFunctions.js"
2. Added a getMostRecentJuly1st function to the CustomDateFunctions.js filefunction getMostRecentJuly1st() {
var today = new Date();
var currentYear = today.getFullYear();
// Create a date for July 1st of the current year
var july1st = new Date(currentYear, 6, 1); // Month is 6 because January is 0
// If today's date is before July 1st, use last year's July 1st
if (today < july1st) {
july1st = new Date(currentYear - 1, 6, 1);
}
var year = july1st.getFullYear();
var formattedDate = '07/01/' + year;
return formattedDate;
}3. Added a "Formula Script File" element to the report and populated the Script File value with "CustomDateFunctions.js" (file added above)
4. I was then able to reference the getMostRecentJuly1st() function to set the default value in the date input element:0
Please sign in to leave a comment.
Comments
1 comment