Get display value of date field in client script

Divya V
Tera Contributor

I have date field in the catalog form for which I am trying to get display value. I have tried 

var date = g_form.workation_start_data.getDisplayValue() and var req = g_form.getValue('workation_start_data'); both are giving empty value.
I have also tried 
var date_number = getDateFromFormat(g_form.getValue('workation_start_date'), g_user_date_format);
var my_date = new Date(date_number);
which gives the output as below
Tue Sep 05 2023 17:08:15 GMT+0530 (India Standard Time)
 
I have to compare the date field value on the form with date field value form the RITMs related to this catalog to allow users to submit the ticket.
 
Could anyone please help me on getting the date value from the date field in client script in 2023-09-05(yyyy-mm-dd) format.
 
Thank you
1 ACCEPTED SOLUTION

Hello @Divya V 

 

Here's how you can modify your script to format a date in 'yyyy-mm-dd' format within a client-side script:

 

 

// Get the date field value as a string
var dateValue = g_form.getValue('workation_start_date');

// Create a JavaScript Date object from the date string
var jsDate = new Date(dateValue);

// Format the date to 'yyyy-mm-dd' format
var formattedDate = jsDate.getFullYear() + '-' +
                   ('0' + (jsDate.getMonth() + 1)).slice(-2) + '-' +
                   ('0' + jsDate.getDate()).slice(-2);

// Now 'formattedDate' contains the date in 'yyyy-mm-dd' format
alert(formattedDate); // Display the formatted date

 

 

 

Output.pngIn this script, we use JavaScript's Date object to manipulate the date value and format it in 'yyyy-mm-dd' format. The g_form.getValue('workation_start_date') method is used to get the value of the date field.

 

Please Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.

 

Thanks & Regards,

Kartik Magadum

View solution in original post

6 REPLIES 6

Kartik Magadum
Kilo Sage

Hello @Divya V 

 

To get the date field value from the catalog form in the 'yyyy-mm-dd' (2023-09-05) format in a ServiceNow client script, you can use the following code:

 

 

// Get the date field value as a GlideDateTime object
var dateField = g_form.getGlideUIElement('workation_start_date');
var dateValue = dateField.getDisplayValue();

// Convert the date to 'yyyy-mm-dd' format
var formattedDate = gs.dateGenerate(dateValue);
formattedDate = formattedDate.split(' ')[0]; // Extract the date part

// Now 'formattedDate' contains the date in 'yyyy-mm-dd' format
alert(formattedDate); // Display the formatted date in the system log

 

This code should give you the date value in the 'yyyy-mm-dd' format. You can use formattedDate for your comparison or any other operations you need in your client script.

Make sure you replace 'workation_start_date' with the actual name of your date field in the catalog form.

 

Please Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.

 

Thanks & Regards,

Kartik Magadum

Hi Kartik Magadum,

 

Client script is not allowing to use gs object

 

Thank you,

Divya

Hello @Divya V 

 

Here's how you can modify your script to format a date in 'yyyy-mm-dd' format within a client-side script:

 

 

// Get the date field value as a string
var dateValue = g_form.getValue('workation_start_date');

// Create a JavaScript Date object from the date string
var jsDate = new Date(dateValue);

// Format the date to 'yyyy-mm-dd' format
var formattedDate = jsDate.getFullYear() + '-' +
                   ('0' + (jsDate.getMonth() + 1)).slice(-2) + '-' +
                   ('0' + jsDate.getDate()).slice(-2);

// Now 'formattedDate' contains the date in 'yyyy-mm-dd' format
alert(formattedDate); // Display the formatted date

 

 

 

Output.pngIn this script, we use JavaScript's Date object to manipulate the date value and format it in 'yyyy-mm-dd' format. The g_form.getValue('workation_start_date') method is used to get the value of the date field.

 

Please Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.

 

Thanks & Regards,

Kartik Magadum

Tushar
Kilo Sage
Kilo Sage

Hi @Divya V 

 

Please try below -

// Get the date field value
var dateValue = g_form.getValue('workation_start_date');

// Check if the dateValue is not empty
if (dateValue) {
    // Create a GlideDate object from the dateValue
    var glideDate = new GlideDate();
    glideDate.setValue(dateValue);

    // Get the date in 'yyyy-MM-dd' format
    var formattedDate = glideDate.getDisplayValue();

    // Now, formattedDate contains the date in 'yyyy-MM-dd' format
    // You can use it for comparisons or other operations
    alert('Formatted Date: ' + formattedDate);
} else {
    alert('Date field is empty');
}

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar