Convert UTC date to Local

by Lance Larsen

HTML

<script src="https://rawgithub.com/timrwood/moment/2.2.1/min/moment.min.js"></script>

JavaScript

// NOTE - if you get "moment is not defined" error - under the "External Resources" in JSFiddle 
// you need to re-add "https://rawgithub.com/timrwood/moment/2.2.1/min/moment.min.js"

var L2date = function () { };

var regexMatchForIso8601Date = /[0-9]T[0-9]/; // ISO 8601 Formatted Date, e.g. 2013-08-24T08:00:00-00:00
var invalidDateMessage = 'Invalid Date';
var dateFormat = 'M/D/YYYY h:mm:ss A';
var date = new L2date();

//--> Works in newer browsers that are able to date.parse ISO 8601 formatted dates
//L2date.prototype.convertUtcToLocalDisplay = function (value) {
//    if (regexMatchForIso8601Date.test(value)) return new Date(value).toLocaleString();
//    return new Date(value + ' UTC').toLocaleString();
//};

L2date.prototype.convertUtcToLocalDisplay = function (value) {
    // Check that the date is not in ISO 8601 format, if not - then add postfix so can be converted to UTC
    if (!regexMatchForIso8601Date.test(value)) value = value + ' UTC';
    if (!moment(value).isValid()) return invalidDateMessage;
    return moment(value).format(dateFormat).toLocaleString();
};

L2date.prototype.convertUtcToLocalDateDisplay = function (value) {
    return date.convertUtcToLocal(value).toLocaleDateString();
};

L2date.prototype.convertUtcToLocalTimeDisplay = function (value) {
    return date.convertUtcToLocal(value).toLocaleTimeString();
};

L2date.prototype.convertUtcToLocal = function (value) {
    return new Date(date.convertUtcToLocalDisplay(value));
};

L2date.prototype.getOffsetHours = function() {
    return date.getOffsetMins() / 60;
};

L2date.prototype.getOffsetMins = function() {
    return -1 * new Date().getTimezoneOffset();
};

L2date.prototype.timeAgo = function(min, max) {
    if (min && max) {
        if (!moment(min).isValid()) return invalidDateMessage;
        if (!moment(max).isValid()) return invalidDateMessage;
        return moment(min).fromNow(true) + " and " + moment(max).fromNow();
    }
    else if (min) {
        if...