Get Date Range

vidhya_mouli
Giga Sage

I am trying to display date in range of 3. Any month between Jan-Mar should take start as Jan and end as Mar.

 

	var today = new GlideDateTime();	

	today.addDays(-today.getDayOfMonthLocalTime() + 1);
	var begin = today.getDate() + " " + "00:00:00";


	var endOfMonth =  new GlideDateTime();
	endOfMonth.addMonthsLocalTime(2)
	endOfMonth.addDays(-1);
	var end = endOfMonth.getDate() + " " + "23:59:59";
gs.log("BEGIN: " + begin);
gs.log("END: " + end);

 

Output:

*** Script: BEGIN: 2024-11-01 00:00:00
*** Script: END: 2025-01-05 23:59:59

 

Expected Output:

*** Script: BEGIN: 2024-10-01 00:00:00
*** Script: END: 2025-12-31 23:59:59

 

How do I adjust my code.

 

 

1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

There's probably a better way to do this, but this is one way:

var today = new GlideDateTime();
var start = new GlideDateTime();
var end = new GlideDateTime();
var todayYear = today.getYearUTC();
var todayMonth = today.getMonthUTC();
var startMonth = 0;
var endMonth = 0;
switch (todayMonth) {
	case 1:
	case 2:
	case 3:
	  	startMonth = 1;
	  	endMonth = 3;
	    break;
	case 4:
	case 5:
	case 6:
		startMonth = 4;
	  	endMonth = 6;
	    break;
	case 7:
	case 8:
	case 9:
		startMonth = 7;
	  	endMonth = 9;
	    break;
	case 10:
	case 11:
	case 12:
		startMonth = 10;
	  	endMonth = 12;
	    break;
}
start.setMonthUTC(startMonth);
start.setDayOfMonthUTC(1);
end.setMonthUTC(endMonth);
end.setDayOfMonthUTC(end.getDaysInMonthUTC());
start = start.getDate() + " " + "00:00:00"; 
end = end.getDate() + " " + "23:59:59";
gs.print("BEGIN: " + start);
gs.print("END: " + end);