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

Hi Tushar,

 

There is an error message while client script is executing saying "GlideDate is not defined"

 

Thank you,

Divya

Naga Ravindra R
Kilo Sage

Hi @Divya V 

 

Write on Submit, client script, calling the Script include using GlideAjax. You need to pass the Date field parameter to script include, have the comparison inside script include.

 

Then do the next process which will be easy for you to allow user to submit or not. 

 

Please mark as helpful or correct, if it helps.