Skip to content

Instantly share code, notes, and snippets.

@Sinetheta
Created March 12, 2012 16:07
Show Gist options
  • Save Sinetheta/2023018 to your computer and use it in GitHub Desktop.
Save Sinetheta/2023018 to your computer and use it in GitHub Desktop.
JS: Sharepoint helpers
// ++ Create Date from SP time string
//----------------------------------------------
function realDate(spString) {
//"yyyy-mm-ddThh:mm:ss-08:00"
var dateArr = spString.split(/\D/),
date = new Date();
date.setFullYear(parseInt(dateArr[0], 10), ((parseInt(dateArr[1], 10) - 1) % 12), parseInt(dateArr[2], 10));
date.setUTCHours(parseInt(dateArr[3], 10), parseInt(dateArr[4], 10), parseInt(dateArr[5], 10));
return date;
}
// ++ Transform SP time string into "time since"
//----------------------------------------------
function timeSince(spString) {
//"yyyy-mm-ddThh:mm:ss-08:00"
var dateArr = spString.split(/\D/),
date = new Date(),
units = ['year', 'month', 'week', 'day', 'hour', 'minute', 'second'],
length = [31536000, 2592000, 604800, 86400, 3600, 60, 1],
seconds, interval, test, i = 5;
date.setFullYear(parseInt(dateArr[0], 10), ((parseInt(dateArr[1], 10) - 1) % 12), parseInt(dateArr[2], 10));
date.setUTCHours(parseInt(dateArr[3], 10), parseInt(dateArr[4], 10), parseInt(dateArr[5], 10));
seconds = Math.floor((new Date() - date) / 1000);
test = seconds;
do {
interval = test;
if (test = Math.floor(seconds / length[i])) {
i--;
}
} while (test && i)
return interval === 1 ? interval + " " + units[i + 1] + " ago" : interval + " " + units[i + 1] + "s ago";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment