
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
06-21-2019 01:39 PM - edited 07-25-2025 06:50 AM
Como calcular diferença de datas em DIAS?
How to Calculate Date Differences in DAYS?
Cómo Calcular Diferencias de Fechas en DÍAS?
var dateOne = new Date(2019, 06, 21); //Year, Month, Date
var dateTwo = new Date(2019, 06, 22);
compareDates(dateOne , dateTwo)
function compareDates(dateOne , dateTwo)
{
if (dateOne > dateTwo) {
gs.print("Date One is greater than Date Two.");
}else {
gs.print("Date Two is greater than Date One.");
}
}
dateDiff / subtract
var gdt1 = GlideDateTime('2015-07-24 18:58:00');
var gdt2 = GlideDateTime('2015-07-24 19:01:00');
var duration2 = GlideDateTime.subtract(gdt1, gdt2);
Duration calculation between start and end date in a scoped application is not working By Adith Sanjay
var start = 28-05-2023 08:21:54';
var removeStartSpace = start.split(" ");
var startDate = removeStartSpace[0].split('-');
var finalStart = startDate[2]+"-"+startDate[1]+"-"+startDate[0]; gs.info(finalStart);
var end = '04-06-2023 08:21:54';
var removeEndSpace end.split(" ");
var endDate = removeEndSpace[0].split('-');
var finalEnd = endDate[2]+"-"+endDate[1]+"-"+endDate[0]; gs.info(finalEnd);
var startDay = this.getParameter('sysparm_start');
var endDay = this.getParameter('sysparm_end');
var start = new GlideDateTime (finalStart);
var end = new GlideDateTime(finalEnd);
var diff = GlideDateTime.subtract(start, end);
var days diff.getRoundedDayPart();
gs.info(days);
Calculate the amount of days between two dates by Claude DAmico
//Strings must be converted to date/time format for getNumericValue()
//Dates must also be converted to have a time value
/*
var enteredDate = new GlideDateTime(gr.getValue('stage_date') + " 00:00:00"); //Assuming field is a string value in the format "01/23/2023"
var enteredDate = new GlideDateTime(gr.getValue('stage_date')); //Assuming field is a date only field
*/
var enteredDateValue = enteredDate.getNumericValue(); //This returns in milliseconds
//Getting the local date/time was done correctly
var gdt = new GlideDateTime();
var currDate = gdt.getLocalDate();
var dateDiff = enteredDateValue - currDate; //Still in milliseconds here
var daysDiff = dateDiff / 24 / 60 / 60 / 1000; //Convert to days. Use Math.abs(dateDiff) to avoid negative values if needed.
Duration Field KB0780039 / KB0754082
(dataChange || current.business_duration.nil())
current.business_duration = gs.calDateDiff(opened, closed, false);
Function field using datediff, task.due_date, and the current date By David Hepburn
- Script Summary
- Fix script to calculate difference between planned start date and current date of a change request
- The Secrets of GlideDateTime
- GlideDateTime
- KB0594663 dateDiff
- KB0831534 DateDiff function produces output by less than a day than expected when used in transform scripts
- KB0824807 subtract(GlideDateTime start,GlideDateTime end) method provides wrong result when second argument passed is null
- KB0823046 How to calculate the duration between 2 days with Scheduler function in Script
- KB0780039 How the Duration and Business Duration calculation works
- KB0754082 How do we calculate the incident duration field?
- https://macul.medium.com/scripts-comparedates-c8c0aebd8f21
Was useful, please leave your feedback!
Participe, entre nas comunidades, acompanhem os posts:
- https://www.youtube.com/@servicenowbr/
- https://www.facebook.com/groups/servicenowbrasil
- https://www.servicenow.com/community/brazil-snug/tkb-p/snug-br-brazil-tkb-board
- https://www.linkedin.com/groups/5134493/
- https://www.servicenow.com/community/user/viewprofilepage/user-id/73505
- https://github.com/Tiagomacul/
- https://www.tiktok.com/@servicenowbr
- https://open.spotify.com/show/1Qa4xVz7xXnKM9y9wggfT9
- https://join.slack.com/t/servicenowbrasil/shared_invite/zt-2sooa78s7-MWwcMxEdbktNjjIYRZfqHg
- https://www.servicenow.com/community/user/viewprofilepage/user-id/73505
- https://www.linkedin.com/in/tiagomacul/
- 1,751 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Interesting! I've normally converted the dates to numeric integers using getNumericValue before comparing them. I didn't realize you could safely do the comparison with non-numeric values like this. Then again, I usually work with GlideDateTime objects. I'm not sure if your script would work with those.