// ServiceNow

Finding the difference between two dates in ServiceNow

The following structure can be used to find the difference between a date and today.

var sourceDate = "01/04/2021"; // test
//sourceDate = grd.variables.sf_effective_date.getValue();

var toDay = new GlideDate();
var sDate = new GlideDate();
sDate.setValue(sourceDate);


//işlem tarihi - bugün
if (findDif(sDate.getByFormat('dd/MM/YYYY'),toDay.getByFormat('dd/MM/YYYY')) >= 0)
    rec.r = true;
//gs.print('VAKTİ GELDİ');
else
    rec.r = false;
//gs.print('İŞLEM YOK');

function findDif(d1, d2) {

    var diffSeconds = gs.dateDiff(d1, d2, true);
    var convertsecondstodays = parseInt(diffSeconds) * 60 * 24;

    return convertsecondstodays;

}

As another alternative, the following code can also be used.

        sourceDate = grd.variables.sf_effective_date.getValue(); //tarih ör: 2020/12/12

        var toDay = new GlideDate(); //bugünün tarihi
        var sDate = new GlideDate();
        sDate.setValue(sourceDate);

var dur = new GlideDuration();
dur = GlideDateTime.subtract(toDay,sDate).getDayPart();
gs.log('tarih farkı:' + dur);

 

// JOIN THE CONVERSATION

Share your experience

Bir sorunun, farklı bir çözümün veya eklemek istediğin bir notun var mı? Giriş yapmadan da yazabilirsin.

// KEEP READING

One more note.

All posts ↗︎