How to validate if the current Month and year is between the start and end date which is in year and Month format(yyyyMM)

Ashwini29
Kilo Contributor

Hi There,

I have a requirement where if the user select month and year from a calendar for viewing a report, we should validate if the selected month falls between his actual start date and end date(u_start_date and u_end_date).

I have written a script to extract the year and month from start and End date, however I require help in writing a condition where the selected month and year is between the month and year of start and end date.

Example:

Selected date = 202204 // 2022 April (yyyyMM format)

Startdate = 20196 // 2019 June

End date = 20225 // 2022 May 

then it should return true if the selected date is in between Startdate and End date. In the given example, the condition should return true. If the End date is 2022 March, it should return false.

Appreciate your help.

Thank you,

Ashwini Y

 

4 REPLIES 4

Jan Cernocky
Tera Guru

Hi,

your dates are somehow inconsistent ... selected date is 04 for April but Start and End date do not have the leading zero in the month. Plus you did not mention whether his needs to be done in server or client side. It might have been easier using GlideDate on server side, but if you are just working with strings the solution can be following (you may adjust the month conditions from < to <= if needed)

var selectedDate = '202204'; // 2022 April (yyyyMM format)
var startDate = '20196'; // 2019 June
var endDate = '20225'; // 2022 May 

var selectYear = parseInt(selectedDate.substring(0,4),10);
var startYear = parseInt(startDate.substring(0,4),10);
var endYear = parseInt(endDate.substring(0,4),10);

var selectMonth = parseInt(selectedDate.substring(4,6),10);
var startMonth = parseInt(startDate.substring(4,6),10);
var endMonth = parseInt(endDate.substring(4,6),10);

gs.info(startYear + ' ' + startMonth);
gs.info(selectYear + ' ' + selectMonth);
gs.info(endYear + ' ' + endMonth);

var match = false;
var matchleft = false;
var matchright = false;

if (startYear < selectYear) {
    matchleft = true;
}
if (selectYear < endYear) {
    matchright = true;
}
if (startYear == selectYear) {
    if (startMonth < selectMonth) {
        matchleft = true;
    }
}
if (selectYear == endYear) {
    if (selectMonth < endMonth) {
        matchright = true;
    }
}

match = matchleft && matchright;

gs.info(match);

 

Hi Jan,

 

Many thanks for your response. It did work. 

I require one more help where I will have to validate the date format based on the input. Say for example,

if(selectedDate = yyyyMM) {

// validate selectedDate is in between start and end date. (Start and end date is in format dd/mm/yyyy)

}

if(selectedDate = DD/MM/YYYY) {

// validate selectedDate is in between start and end date. (Start and end date is in format dd/mm/yyyy)

}

 

I am working this on Server-side(Script Include)

Can you clarify where the date is coming from? I assume it is a form field? Is it a string or date field?

Also, are you doing this check on a form (client script) or database (server side)? 

It is important to check the best method 

Hi Jan,

Thanks for your response. The user selects the date from Service Portal widget.

The field type of "start and End date" are "Date" and the field type of "Selected Date" is "String" and I am doing this check on Server side.

Thank you,

Ashwini Y