Change the date format to 'yyyy-mm-dd' even if user enters date manually

aamir1
Mega Expert

Hi,

I need to change the date format to 'yyyy-mm-dd' of a date type field even if user enters date manually.

I tried with getByFormat but it is returning wrong year. Kindly help.

 

Thanks

1 ACCEPTED SOLUTION

Hi,

Please use MM instead of mm, and it will work.

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

View solution in original post

8 REPLIES 8

Thanks Asif, This is working fine for 'MM-dd-yyyy' format. But when i tried the same for 'yyyy-mm-dd'. It is not working. Returning 00 for month.

Hello aamir,

Can you share your current date format in which you are getting and the script you are trying. We need to first convert that to 1 format as input and then convert to our required format.

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

Hi Asif,

Below is my code-

onchange client script
=============
var ga = new GlideAjax('scriptinclude');
ga.addParam("sysparm_name", "methodname");
ga.addParam("sysparm_sdate", newValue);

script include
==============
var str = this.getParameter('sysparm_sdate');
var todayDateTime = new GlideDateTime(str);
var dateE= todayDateTime.getLocalDate().getByFormat('yyyy-mm-dd');
gs.log("dateE"+dateE);

Hi,

Please use MM instead of mm, and it will work.

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP