Noob JavaScript Date Question

G24
Kilo Sage

Hello, smart people,

Q1) How can I debug this code?  (Line 16 fails)

Bug.png

DateErrorMessage.png

 

I have been relying heavily on the "W3Schools" website.

I am having some difficulty understanding Constructor Number THREE (3) here:

https://www.w3schools.com/jsref/jsref_obj_date.asp

The constructor looks like this:  new Date(dateString);

But I'm using the ISO standard, with dashes, so shouldn't that work???  It's described here:

https://www.w3schools.com/js/js_date_formats.asp 

 

Q2) Why would the ISO date format not work in ServiceNow?

Q3 Where can I find the OFFICIAL reference on this object and this constructor?

Thank you!

5 REPLIES 5

Marco0o1
Tera Sage

Hi @G24 ,

Replace new Date() for "new GlideDate()" that is for servicenow Instance, that should work for you, for more information go to GlideDate .

 

Hope that heelp you

Thanks @Marco0o1 .  I guess I was not very clear in my post, but I don't want to use the Glide objects.  I just want the regular JavaScript objects to work.

Hi @G24 ,

 

In you code you tried to enter a input using a ISO input and you whant to convert to normal date, you can define a function or use this example:

 

function convertISOToDate(isoDate) {
  var dateParts = isoDate.split('-');
  var year = parseInt(dateParts[0], 10);
  var month = parseInt(dateParts[1], 10) - 1; // Months in JavaScript are 0-based
  var day = parseInt(dateParts[2], 10);
  
  return new Date(year, month, day);
}

// Example usage
var isoDateString = "2023-08-25";
var dateObject = convertISOToDate(isoDateString);
gs.log(dateObject); //Output is "Fri Aug 25 2023 00:00:00 GMT-0700 (PDT)"

 

And if you want to enter a normal date and covert to ISOSTring use something like this:

var currentDate = new Date();
gs.log(currentDate.toISOString().split('T')[0]); // Output is 2023-08-25
gs.log(currentDate.toISOString()); // Output is 2023-08-25T14:52:26.941Z

 

 

Hope that help you

Sagar Pagar
Tera Patron

Hi @G24,

 

Take a look at below answers to your questions.

For Q1:

Replace with GlideDateTime()

var gdt = new GlideDateTime("2015-03-25");

gs.info(d.getDate());

 

For Q2:

Take a look at old thread. It will help you.

How to convert ServiceNow ticket created date in to ISO 8601 format? 

 

For Q3:

Refer this: GlideDateTime 

 

If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers! 👍🏻
Thanks,
Sagar Pagar

The world works with ServiceNow