new GlideDateTime().getDate(); this is giving complete date in" YYYY/MM/DD" format, I want only month and year in "MM/YYYY" this format .....what is script for this.

animesh3
Kilo Contributor

 new GlideDateTime().getDate(); this is giving complete date in" YYYY/MM/DD" format, I want only month and year in "MM/YYYY" this format .....what is script for this

5 REPLIES 5

Voona Rohila
Mega Patron
Mega Patron

Hi animesh

Try this code to get month and year seperately.

var x= new GlideDateTime().getDate();
var year = x.getYear();
var month = x.getMonth();

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Yes you can also use getByFormat as suggested by monica


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

monicaa
Tera Contributor

Hello Animesh,

Try using below code:

var a = new GlideDateTime().getDate();

a.getByFormat('MM/YYYY');

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

this should work

var gdt = new GlideDateTime();
var todayDate = new GlideDate();
todayDate.setValue(gdt.getLocalDate());

var myFormat = todayDate.getByFormat("MM/yyyy");
gs.info("My format->" + myFormat);

Output:

find_real_file.png

Regards
Ankur

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