First Day & Last Day of the Month.

SD29
Tera Expert

Hi All,  

How can I get the First day & Last Day of the current month from 2 years before the current day. 

E.g., If the current day is 07/17/2022 then I would like to get the First Day as 07/01/2020 & Last day as 07/31/2020. So that I can search for the data in a table that was created between the 2 dates (First Day & Second day).

Thanks. 

2 REPLIES 2

Yousaf
Giga Sage

Hi SD,

Can you try this please.

var gdate = new GlideDateTime();
gs.info("Current Date: "+gdate.getDate());
gs.info("Current Month: "+gdate.getMonthLocalTime());

var twoYearsAgo = gs.yearsAgo(2);
gs.info("Two years ago: "+twoYearsAgo);
var gr = new GlideDateTime(twoYearsAgo);

var endDate = gr.getYear()+"-"+gr.getMonth()+"-"+gr.getDaysInMonth()+" "+"12:00:00"; 
var firstDate = gr.getYear()+"-"+gr.getMonth()+"-"+gr.setValue(gs.beginningOfThisMonth())+" "+"12:00:00"; 

gs.info("first date of this month: "+firstDate);
gs.info("Last date of this month: "+endDate);

 

Mark Correct or Helpful if it helps.

Thanks,
Yousaf


***Mark Correct or Helpful if it helps.***

Hello Yousaf,

Thanks for your response. 

I tried the script and everything is working as expected but the Firstday of the month is coming in as undefined when I try the script in Background scripts. 

first date of this month:2020-07-undefined 12:00:00

Thanks.