- 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
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-27-2023 06:26 AM
This worked! I was not aware that getNumericValue() worked on strings and not a date format but in retrospect makes sense. Thanks for the help.
- 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
02-27-2023 06:26 AM
Great solution! Thank you for the help, it worked perfectly.