Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Find date is in-between start and end date

Kingstan M
Kilo Sage

Hello - SNC.

 

I want to see if in_between date is in-between the given start/end datetime.

We can take date difference between two but how to find x-date is in-between start and end.

 

Any advice?

 

var g_sch = new GlideRecord('cmn_schedule_span');
g_sch.addQuery('sys_id', 'some_sys_id_here');
g_sch.query();
while (g_sch.next()) {
    var start = g_sch.start_date_time.getDisplayValue();
    var end = g_sch.end_date_time.getDisplayValue();
    var in_between = "18-02-2023 02:30 PM";

    var start_GD = new GlideDateTime(start);
    var end_GD = new GlideDateTime(end);

    var in_betwee = new GlideDateTime();
    in_betwee.setDisplayValue(in_between, "dd-MM-yyyy hh:mm a");
    var in_between_GD = in_betwee.getDisplayValue();

    gs.info(start);
    gs.info(end);
    gs.info(in_between);
    gs.info("---------------------");
    gs.info(start_GD);
    gs.info(end_GD);
    gs.info(in_between_GD);
}

// test data
// start >> 08-02-2023 12:00 AM 
// end >> 30-03-2023 11:59 PM

 

3 REPLIES 3

priyasunku
Mega Sage

@Kingstan M 

 

In the code you can check if x-date is greater than start date and x-date should be less than end date.

 

calculate diff of start date and x-date

 

var dif1= gs.dateDiff(start, x-date, true)

var dif2=gs.dateDiff(x-date, end,true)

 

if(dif1>0 && dif2 >0)

return true;

 

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful

Hemant Goldar
Mega Sage

Hi @Kingstan M,

Here is sample script to find difference between two dates and get date part.

 

var start = new GlideDateTime(current.start_date_time);
var end = new GlideDateTime(current.end_date_time);

var diffInDays = GlideDateTime.subtract(start, end).getDayPart();

 

I hope this helps!

 

Regards,

Hemant 

**If my answer has helped with your question, please mark my answer as an accepted solution and give it a thumbs up.

**

 

Sonu Parab
Mega Sage

Hi @Kingstan M ,
Have a look this post to check if a date falls in between two other dates. 

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!
Thank you