How to get first day of the specific month.

app
Mega Expert

Hello All,

I am trying to get the first day of the specific month.

ex: someone passes (-2 from the current month), so end date  should be 2021-11-31 and start date should be 2021-11-01.
Please let me know how I can get it.

 

Thanks

6 REPLIES 6

Hi,

Please use the script below to get which you are looking for :

var sentDateTime = new GlideDateTime("2021-11-20 12:00:00");
var daysInMonth = sentDateTime.getDaysInMonth(); 
var sentDay = sentDateTime.getDayOfMonth(); 
var firstOfMonth = new GlideDateTime(sentDateTime);   
firstOfMonth.addDays( -1 * (sentDay-1));   

gs.info(firstOfMonth.getDisplayValue()); 

 

This is working for me in my PDI.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

OlaN
Giga Sage
Giga Sage

Hi,

You could try something like this:

var date = new GlideDateTime();
date.addMonths(-2);
gs.info('date minus two months: ' + date);

date.setDayOfMonthLocalTime(1);
gs.info('first day: ' + date);