I suspect you meant this line:
var timeDisplay = moment(element.text).local();
to use textContent
rather than text
:
var timeDisplay = moment(element.textContent).local();
Separately, note that not all browsers support textContent
; some use innerText
instead (which is slightly different, but probably similar enough for what you're doing).
If you're going to use those properties, early in your script you might want to identify which to use, once, and remember that:
var textPropName = 'textContent' in document.createElement('div') ? 'textContent' : 'innerText';
...and then:
var elements = [].slice.call(document.querySelectorAll("a.date_due"));
elements.forEach(function(element) {
var timeDisplay = moment(element[textPropName]).local();
var zone = timeDisplay.format("ZZ");
zone = zone == "-0500" ? zone = "CST" : zone = "GMT";
element[textPropName] = timeDisplay
.format("MMM Do YYYY, ddd, h:mm:ss a ")
+ zone;
});
About converting the return value of querySelectorAll
and using forEach
: As RobG points out, that will fail on IE8 and earlier (which many of us still have to worry about) because A) You can't use slice
like that with host-provided objects on IE8, and B) IE8 doesn't have forEach
unless you shim it. It's also creating objects unnecessarily.
Rather than create the unnecessary array, just use forEach
directly (and shim it if the browser doesn't have it):
Array.prototype.forEach.call(
document.querySelectorAll("a.date_due"),
function(element) {
var timeDisplay = moment(element[textPropName]).local();
var zone = timeDisplay.format("ZZ");
zone = zone == "-0500" ? zone = "CST" : zone = "GMT";
element[textPropName] = timeDisplay
.format("MMM Do YYYY, ddd, h:mm:ss a ")
+ zone;
});
That'll work (if you shim forEach
) since either the browser has native support that isn't broken, or you're using a shim.