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

How to use Moment.js in ServiceNow

yundlu316
Kilo Guru

Our team is trying to use Moment.js in our instance, but can't seem to get it to work.  Here are a couple questions we have about it:

  1. We noticed that there is a dependency out of the box called moment-timezone-with-data-2010-2020-v0.5, is this the same as moment.js?  If so, does this mean we don't need to bring in moment.js as a new dependency?
  2. We tried using the above ootb dependency AND tried to bring in moment.js to use in a widget, and we keep getting a console error saying that moment is undefined.  Can someone provide some instructions on how to correctly get either one of these dependencies to work?
  3. If we wanted to use moment.js on a platform business rule, what do we have to do to make that happen?  Are you able to access a dependency via business rule?

Thanks!

12 REPLIES 12

Hi @priyanga 

 

We can't use the newest versions of MomentJS because it uses modern JavaScript syntax, and ServiceNow's JavaScript engines are too old to understand the syntax changes.

 

Thanks,

Ryan

sadif_raja
Tera Guru

Solution: Using Native JavaScript Date Methods for Date Validation in ServiceNow

In ServiceNow, you can validate date fields without relying on external libraries like moment.js by using JavaScript's built-in Date methods. This solution works natively and simplifies date handling for form submissions.

Here’s how you can implement the solution to validate that the difference between two dates (u_from and u_to) does not exceed six months.

Steps:

  1. Retrieve Date Values: Use g_form.getValue() to fetch the values from your date fields (u_from and u_to).

  2. Parse the Date Values: Use JavaScript's Date object to convert the string values from the form fields into actual date objects.

  3. Calculate the Difference in Months: You can calculate the difference in months by comparing the year and month values from both dates. This approach does not require any external library and works efficiently.

  4. Add Validation Logic: Check if the calculated difference in months exceeds six. If it does, display an error message using g_form.addErrorMessage() and prevent the form from being submitted.

  5. Here's the code for date validation: Use it
  6. function onSubmit() {
    // Get the values of the date fields
    var startDate = g_form.getValue('u_from'); // Replace with your actual field name
    var endDate = g_form.getValue('u_to'); // Replace with your actual field name

    // Ensure both dates are filled
    if (!startDate || !endDate) {
    g_form.addErrorMessage('Please enter both start and end dates.');
    return false;
    }

    // Parse dates using JavaScript's Date object
    var start = new Date(startDate);
    var end = new Date(endDate);

    // Calculate the difference in months
    var diffInMonths = (end.getFullYear() - start.getFullYear()) * 12 + (end.getMonth() - start.getMonth());

    // Check if the difference is 6 months
    if (Math.abs(diffInMonths) >= 6) {
    g_form.addErrorMessage('The gap between the dates should not exceed 6 months.');
    return false; // Cancel form submission
    }

    return true; // Allow form submission
    }

    Explanation:

    • Date Parsing: new Date(startDate) and new Date(endDate) convert the form input values into Date objects.
    • Month Difference Calculation: (end.getFullYear() - start.getFullYear()) * 12 + (end.getMonth() - start.getMonth()) calculates the difference in months by comparing both the year and month values.
    • Validation: If the difference is 6 months or greater, an error message is displayed, and the form submission is canceled.

    Why Use This Solution?

    • No External Dependencies: This method doesn’t require moment.js or any external libraries, making it lightweight and efficient.
    • Native JavaScript: It leverages built-in JavaScript functionality, ensuring compatibility with ServiceNow’s scripting environment.

    This approach is clean, efficient, and works directly within the ServiceNow platform.

    Mark it helpful, if your issue is resolved.

sadif_raja
Tera Guru

 

Solution: Using Native JavaScript Date Methods for Date Validation in ServiceNow

In ServiceNow, you can validate date fields without relying on external libraries like moment.js by using JavaScript's built-in Date methods. This solution works natively and simplifies date handling for form submissions.

Here’s how you can implement the solution to validate that the difference between two dates (u_from and u_to) does not exceed six months.

Steps:

  1. Retrieve Date Values: Use g_form.getValue() to fetch the values from your date fields (u_from and u_to).

  2. Parse the Date Values: Use JavaScript's Date object to convert the string values from the form fields into actual date objects.

  3. Calculate the Difference in Months: You can calculate the difference in months by comparing the year and month values from both dates. This approach does not require any external library and works efficiently.

  4. Add Validation Logic: Check if the calculated difference in months exceeds six. If it does, display an error message using g_form.addErrorMessage() and prevent the form from being submitted.

Here’s the updated code:

function onSubmit() { // Get the values of the date fields var startDate = g_form.getValue('u_from'); // Replace with your actual field name var endDate = g_form.getValue('u_to'); // Replace with your actual field name // Ensure both dates are filled if (!startDate || !endDate) { g_form.addErrorMessage('Please enter both start and end dates.'); return false; } // Parse dates using JavaScript's Date object var start = new Date(startDate); var end = new Date(endDate); // Calculate the difference in months var diffInMonths = (end.getFullYear() - start.getFullYear()) * 12 + (end.getMonth() - start.getMonth()); // Check if the difference is 6 months if (Math.abs(diffInMonths) >= 6) { g_form.addErrorMessage('The gap between the dates should not exceed 6 months.'); return false; // Cancel form submission } return true; // Allow form submission }
 

Explanation:

  • Date Parsing: new Date(startDate) and new Date(endDate) convert the form input values into Date objects.
  • Month Difference Calculation: (end.getFullYear() - start.getFullYear()) * 12 + (end.getMonth() - start.getMonth()) calculates the difference in months by comparing both the year and month values.
  • Validation: If the difference is 6 months or greater, an error message is displayed, and the form submission is canceled.

Why Use This Solution?

  • No External Dependencies: This method doesn’t require moment.js or any external libraries, making it lightweight and efficient.
  • Native JavaScript: It leverages built-in JavaScript functionality, ensuring compatibility with ServiceNow’s scripting environment.

This approach is clean, efficient, and works directly within the ServiceNow platform.