Calculated expression to get the number of days from a column until today
Tryin to get a calculated expression to work instead of using Microsoft OOB calculated column.
- I have a date column called "Date reported".
- I have second column called "Days old". I want the days old column to calculate the number of days from the "Date reported" column until today.
I've been trying to get the calculated expression below to work:
// load the moment.js library from whereever you need it
var url = "https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js";
var fileName = "momentjs";
var moduleCache = window.module;
var defineCache = window.define;
window.module = undefined;
window.define = undefined;
window.SP.SOD.registerSod(fileName, url);
window.LoadSodByKey(fileName, null, true);
window.module = moduleCache;
window.define = defineCache;
//parse the "Date reported" field with moment.js and get an integer representation of it
var dateInt = Date.parse(days([[DateReported]]);
//now create a date object out the integer we got in the previous line
var dateObject = new Number(dateInt);
var days = Number().diff(dateObject, 'days', false);
What am i missing? Any help would be greatly appreciated.
Thanks,
-Jay
-
Hi Jay,
try this:
// load the moment.js library from whereever you need it
var url = "https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js";
var fileName = "momentjs";
var moduleCache = window.module;
var defineCache = window.define;
window.module = undefined;
window.define = undefined;
window.SP.SOD.registerSod(fileName, url);
window.LoadSodByKey(fileName, null, true);
window.module = moduleCache;
window.define = defineCache;
//parse the "Date reported" field with moment.js
var dateReported = moment([[DateReported]]);
var today = moment();
//calculate the difference in days
return today.diff(dateReported, "days");0 -
Now I'm getting this error when trying to evaluate "Error occurred on evaluating expression. Reference window is not defined at line 4"
I don't understand why that error is showing up now. Any thoughts?
-Jay
0 -
That is really strange. I just used your script as a starting point and tweaked the last few lines. When I paste this into the expression-editor as a function it works just as expected.
0
Please sign in to leave a comment.
Comments
3 comments