- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2023 04:42 AM
I have date field in the catalog form for which I am trying to get display value. I have tried
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2023 05:34 AM - edited 09-05-2023 05:45 AM
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
In 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2023 05:14 AM - edited 09-05-2023 05:40 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2023 05:31 AM
Hi Kartik Magadum,
Client script is not allowing to use gs object
Thank you,
Divya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2023 05:34 AM - edited 09-05-2023 05:45 AM
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
In 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2023 05:18 AM
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