- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-24-2023 11:23 AM - edited ‎02-24-2023 11:29 AM
I am returning the date values and current date appropriately but can't seem to figure out how to find the number of days between the two dates. Below are the dateDifference variables I tried using with no success.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-24-2023 04:29 PM
This may entirely have to do with date and time formatting. Try something like what I have below.
//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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-24-2023 04:42 PM
You can use the GlideDateTime object's getNumericValue() method. This method returns the number of milliseconds between two dates, which can be converted to days by dividing the result by the number of milliseconds in a day.
var gr = new GlideRecord('team_members');
gr.query();
while(gr.next()) {
var dateEntered = gr.getValue('stage_date');
var gdt = new GlideDateTime();
var currentDate = gdt.getLocalDate();
var dateDifferenceInMs = currentDate.getNumericValue() - GlideDateTime(dateEntered).getNumericValue();
var dateDifferenceInDays = Math.floor(dateDifferenceInMs / (1000 * 60 * 60 * 24));
gs.info(dateDifferenceInDays);
}
we first calculate the date difference in milliseconds by subtracting the getNumericValue() of the dateEntered variable from the getNumericValue() of the currentDate variable. Then, we convert this value to days by dividing it by the number of milliseconds in a day (1000 * 60 * 60 * 24) and rounding down to the nearest integer using Math.floor(). Finally, we log the result using gs.info().
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2023 05:34 AM
Yes! this solution works
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-20-2024 10:50 PM
Thanks. It was helpful
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2025 11:26 PM - edited ‎02-27-2025 11:26 PM
To calculate the number of days between two dates, you need to convert both dates into a common format, like milliseconds, and then subtract one from the other. Afterward, divide the result by the number of milliseconds in a day (86,400,000) to get the difference in days. For an easy-to-use solution, you can try using an Online Date Counter to quickly calculate the days between two dates. In your case, try using the getNumericValue() method to get the dates in numeric form (milliseconds), subtract them, and then convert that result into days. This will give you the exact number of days between the entered date and the current date.