Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get the name of previous month with year (no time required) using javascript or any other method

suprakash
Giga Expert

I need to get the name of previous month with year (no time required) .

My requirement is to display it on scheduled report subject.

Like for example :   writing this on the subject   javascript:"This report is   for " + gs.now();

it displays the current date

. but i have to send the report for previous month.If the month is may 2017 .Then the subject should be like This report is for the month of April 2017.

How can i do this??

And if you can help me with how to write this thing in report header then it will be greatly helpful.

var monthNames = ',December,January,February,March,April,May,June,July,August,September,October,November'.split(',');

var gdt = new GlideDateTime();

var month=(monthNames[new GlideDate().getMonthLocalTime()]+":"+gdt.getYear());

It can be done by this code but how to put in the subject i am not getting any way

1 ACCEPTED SOLUTION

bubuprasadswain
Tera Guru

Write a script include as below:


function month_year_call() {


  var monthNames = ',December,January,February,March,April,May,June,July,August,September,October,November'.split(',');


var gdt = new GlideDateTime();


var month=(monthNames[new GlideDate().getMonthLocalTime()]+":"+gdt.getYear());


return month;


}



Subject line in report as: javascript:"This report is   for " +month_year_call();



Mark Correct if this solves your issue and also hit Like and Helpful.


Regards,


Bubuprasad


View solution in original post

8 REPLIES 8

Hi All,

Please try this also:-

function test1(){
var gdt = new GlideDateTime();
gdt.addMonths(-1);
return (gdt.getDate().getByFormat("MMM-yyyy"));
}

Thanks,
Saikiran Guduri (NOW)
(Please mark the answer as correct answer/helpful/accept the sollution if it helps)

var gr = new GlideDate();
gs.info(gr.getMonthUTC()) // returns current month
gs.info(gr.getMonthUTC()-1) // returns previous month

BALAJI40
Mega Sage

Use this script,



var monthNames = [ "January", "February", "March", "April", "May", "June",

  "July", "August", "September", "October", "November", "December" ];


var date = new GlideDateTime();
date = monthNames[date.getMonthLocalTime()-2]+":"+date.getYear();


If you want to call it as in email subject, create one mail script and call this notification body like this way,



var monthNames = [ "January", "February", "March", "April", "May", "June",  

      "July", "August", "September", "October", "November", "December" ];  


var date = new GlideDateTime();
date = monthNames[date.getMonthLocalTime()-2]+":"+date.getYear();
email.setSubject('This is the report of previous month '+date);


call this in notification body: ${mail_script:<mail script name>}