A ‘Last Updated’ column would have been cool feature for Sharepoint 2010 Lists. One look at the list and you would know how much time has elapsed since the items were updated. Something like, ‘This list item was updated 3 days ago’.
Makes life a lot easier, doesn’t it?
In search of an answer, I stumbled across this:
I followed the instructions exactly, but kept getting an error ‘NaN Min’. Apparently, the code works like a charm in Sharepoint 2007 but not in SP 2010. Now, what should we do? Debug, of course!
NaN stands for ‘Not a number’. Obviously, the JQuery was not reading the date string in the ‘Last Updated’ column correctly. I realized that this issue was quite common in SP 2010 and got down to solving it.
The solution: A bit of code needs to be added to parse the default value as Date.
This is the modified code:
var str = “Last Updated”;
var today = new Date();
today = Date.parse(today)/1000;
var headers = $(“table.ms-listviewtable:first> tbody> tr:first th”).get();
x = $(e).contents().find(“a[title*='”+str+”‘]”).length;
a = x > 0 && i > a ? i : a;
var dArray = $(“table.ms-listviewtable:first> tbody> tr:gt(0)”).find(“>td:eq(“+a+”)”).get();
var MyDate= $(e).text();
var MD_M=MyDate.substring(3,5); // Jan-Dec=01-12
var MD_M=MD_M-1; // Jan-Dec=00-11
var Dobj=new Date(MD_Y, MD_M, MD_D, MD_H, MD_N, MD_S);
var d1 = Date.parse(Dobj)/1000;
var dd = (today-d1)/86400;
var dh = (dd-Math.floor(dd))*24;
var dm = (dh-Math.floor(dh))*60;
var time = ((Math.floor(dd) > 0 ? Math.floor(dd) +” days, ” : “”)+
(Math.floor(dh) > 0 ? Math.floor(dh)+” hrs, ” : “”)+
How to deploy this to your Sharepoint list:
- Add a column ‘Last Updated’ to your Sharepoint list.
- In the list view, make sure that the ‘Last Updated’ column is the first column from the right.
- Add a content editor to your List view page and copy the code into the HTML source.
- Save your changes and enjoy.
Glad I could help!
- In case you want some other name for the column instead of ‘Last Updated’, change the value for the variable ‘str’ in the script.
- If you do not want to keep ‘Last updated’ as the rightmost column, you can hard code the column number into the variable ‘a’.
A big thanks to Paul and his script.