Help with Date field

User485076
Tera Contributor

Hello, 

I have a date field on a catalog form. I basically need to get the value of the date in this field and today's date and then do some calculations. However, when I log the dates, I'm getting it in the wrong format and time zone. Screenshot attached, I need the scheduled date to show in the same format as todays date.

 

 var selectedDate = new Date(newValue);
  console.log("selected date is: ", selectedDate);
  var today = new Date();
  console.log("today date: ", today);
 
User485076_0-1704714771085.png

 

12 REPLIES 12

Hi Amit, 

 

This is coming out as below. Any way of getting to show the same format as scheduled date? Should be 08/01/2024

User485076_0-1704723169805.png

 

Hi @User485076 

 

Please use below code:

 

var today_date = new Date();
var day = today_date.getDate();
day = day < 10 ? "0" + day : day;
var month = today_date.getMonth();
month = month + 1;
month = month < 10 ? "0" + month : month;
var year = today_date.getFullYear();
var today_date_str = day + "/" + month + "/" + year;

 

Thanks & Regards

Amit Verma

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Ankur Bawiskar
Tera Patron
Tera Patron

@User485076 

don't use Date() class of javascript

If you are using catalog client script then pass this value to script include function via GlideAjax and then perform the calculations and return the value back to client script

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader