Showing results for 
Show  only  | Search instead for 
Did you mean: 
Dynatrace Participant
Dynatrace Participant

Steps to follow to perform Dynamic Date Selection with help of JavaScript Event on browser click path synthetic monitor,

While working with Travel related websites, we may have the scenario to select the From & To Date input fields dynamically and this below JavaScript code will do the same.

var now = new Date();
var today = new Date(now.getFullYear(), (now.getMonth()), now.getDate());
var oneMonthLater = new Date(today.valueOf() + 30*1*24*60*60*1000); //30 days later from the current date; 

var ReturnDate = new Date(today.valueOf() + 37*1*24*60*60*1000);; //37 days later from the current date

function monthConverter(month){     switch(month){         case 0: return '01';         case 1: return '02';         case 2: return '03';         case 3: return '04';         case 4: return '05';         case 5: return '06';         case 6: return '07';         case 7: return '08';         case 8: return '09';         case 9: return '10';         case 10: return '11';         case 11: return '12';     } }

var startMonth = monthConverter(oneMonthLater.getMonth());
var startDay = ("0" + oneMonthLater.getDate()).slice(-2);
var startYear = oneMonthLater.getFullYear();
var returnMonth = monthConverter(ReturnDate.getMonth());
var returnDay = ("0" + ReturnDate.getDate()).slice(-2);
var returnYear = ReturnDate.getFullYear();
var departDate = startYear  + "-" + startMonth+ "-" + startDay;  // change the variable order format and delimiter based on your input filed based on your date format for both departDate & returnDate variables. In this example date format is dd-mm-yyyy
var returnDate = returnYear  + "-" + returnMonth + "-" + returnDay;;;
function triggerevent(element) {
    var evt = document.createEvent("MouseEvent");
    var eventType = ["input", "change"];
    for (i = 0; i < eventType.length; i++) {
        evt.initMouseEvent(eventType[i], true, true, window);
        element.dispatchEvent(evt);  } }

var element1 = document.querySelector("#From"); // Replace with From input field JS Path
var element2 = document.querySelector("#To");   // Replace with To input field JS Path
element1.value = departDate;
element2.value = returnDate;
  1. Go to the relevant page on Chrome browser

  2. Right click on the From & To Date input elements which you want to perform the dynamic date selection on the page-> Inspect the element which will open the developer tools



  3. Right click on the element on the Web developer tools - > Copy - > Copy JS path of both From & To Date input elements



  4. Paste the JS path on the below element1 & element2 value and add this JavaScript code into the JavaScript Event of your Browser click path monitor which will enter the Dynamic Date on From & To input fields as shown in the below image,



Note:- Change the departDate & returnDate variable assigned order format and delimiter based on your input filed date format. In this example date format is dd-mm-yyyy

Version history
Last update:
‎11 Apr 2023 07:19 AM
Updated by:
DynaMight Legend
DynaMight Legend

Thank you for sharing this! I'm sure it will help out many community members.