The CreatorCon Call for Content is officially open! Get started here.

The below script working fine for some region people but not some other region people.

Deepa12
Tera Contributor

Hi,

Below is the catalog client script written to check termination date should not exceed to next 5 working days.

 

THe below script working fine for some region people but not some other region people.

Catalog client script:

function onSubmit() {
var val = g_form.getValue("termination_date");
var dateObjectNow = new Date();
var dateObjectNew = new Date(val);

var dateNow = Math.floor(dateObjectNow.valueOf() / (1000 * 60 * 60 * 24));
var dateNew = Math.floor(dateObjectNew.valueOf() / (1000 * 60 * 60 * 24));

var dayOfWeek = dateObjectNew.getDay();
var date = dateNew - dateNow;

var msg;
if (date >= 0 && date < 8 && dayOfWeek != 5 && dayOfWeek != 6) {
g_form.hideFieldMsg('termination_date', true);
} else {
g_form.showFieldMsg('termination_date', 'termination date error message', 'error');
return false;
}

}

3 REPLIES 3

Sohithanjan G
Kilo Sage

Hi @Deepa12 

The issue you're encountering might be related to the different timezones of the users. The code you provided calculates the difference in days between the current date (dateObjectNow) and the selected termination date (dateObjectNew) using the valueOf() method, which returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.

Since JavaScript Date objects work with UTC time by default, the difference in days might vary depending on the user's timezone. For example, if a user in a different timezone submits the form with the same termination date, the calculated difference in days could be different due to the timezone offset.

To resolve this issue, you should adjust the script to use the local time of the users when comparing dates. You can use the setHours() method to set the time to midnight (00:00:00) for both dateObjectNow and dateObjectNew to ensure they are compared using local time.

Here's the updated code:

 

function onSubmit() {
  var val = g_form.getValue("termination_date");
  var dateObjectNow = new Date();
  var dateObjectNew = new Date(val);

  // Set hours to midnight (00:00:00) for both date objects
  dateObjectNow.setHours(0, 0, 0, 0);
  dateObjectNew.setHours(0, 0, 0, 0);

  var dateNow = Math.floor(dateObjectNow.valueOf() / (1000 * 60 * 60 * 24));
  var dateNew = Math.floor(dateObjectNew.valueOf() / (1000 * 60 * 60 * 24));

  var dayOfWeek = dateObjectNew.getDay();
  var date = dateNew - dateNow;

  var msg;
  if (date >= 0 && date < 8 && dayOfWeek !== 5 && dayOfWeek !== 6) {
    g_form.hideFieldMsg('termination_date', true);
  } else {
    g_form.showFieldMsg('termination_date', 'termination date error message', 'error');
    return false;
  }
}

 

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

Thanks for replying back to me. This issue occur because of user profile system date format(default format YYYY-MM-DD). The above script is working for the default date format. This script doesnt work correctly if i change the date format like - dd.mm.yyyy into user profile.

 

Deepa12_0-1691509472571.png

Please assist me to change the script to support all the date format.

Sohithanjan G
Kilo Sage

Hi @Deepa12 ,

You can use below code to change date format

 

// Input date in "YYYY-MM-DD" format
var inputDate = "2023-08-10";

// Parse the input date
var dateParts = inputDate.split("-");
var year = dateParts[0];
var month = dateParts[1];
var day = dateParts[2];

// Create a new date in the desired format "dd.mm.yyyy"
var formattedDate = day + "." + month + "." + year;

console.log(formattedDate); // Output: "10.08.2023"

 

Please mark my answer or accept the solution if it suffices your requirement. Thanks

 

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)