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

Siddartha Gudim
Tera Guru

Hi Suprakash,



You can use service now date function like beginningOfLastMonth() to achieve this requirement. The function returns the date and time of beginning of the previous month. The output of the function will be in the format of yyyy-mm-dd hh:mm:ss. Since you need no time you can convert the date into string and analyse the result.




Please mark helpful/Correct if you find this useful.




Regards,


Siddartha Gudimalla.


@Siddartha Gudimalla  could you help with format for  conversion to of date to  string:

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


Thanks Bubuprasad ,


Its working greatly .Just one more favor is there any way that we can implement the same in the Pdf page header footer template ???????


so that we donot need to use it in the subject . it will display it in the pdf itself