StackExchange sites - convert dates to local timezone

Converts dates to your local timezone

La data de 15-12-2015. Vezi ultima versiune.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        StackExchange sites - convert dates to local timezone
// @namespace   darkred
// @description Converts dates to your local timezone
// @match       *://*.stackoverflow.com/*
// @match       *://*.stackexchange.com/*
// @match       *://*.superuser.com/*
// @match       *://*.stackapps.com/*
// @match       *://*.askubuntu.com/*
// @match       *://*.mathoverflow.net/*
// @match       *://*.serverfault.com/*
// @version     1
// @grant       none
// @require     https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment-with-locales.min.js
// @require     https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.4.1/moment-timezone-with-data.min.js
// @require     https://cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.6/jstz.min.js
// ==/UserScript==



var dates = document.getElementsByClassName('relativetime');


// tooltip with added timezone: "2015-12-14 14:11:13 +0000"
function toTimeZone(time, zone) {
    var format1 = ('YYYY-MM-DD HH:mm:ss Z');
    var format2 = ('YYYY-MM-DD HH:mm:ss');
    return moment(time, format1).tz(zone).format(format2);
}


function getLocalTimezone(){
    var tz = jstz.determine();    // Determines the time zone of the browser client
    return tz.name();             // Returns the name of the time zone eg "Europe/Berlin"
}


var localTimezone = getLocalTimezone();


function convertTitleDates() {
    var temp;


    for (var i = 0; i < dates.length; i++) {

        // "2015-12-14 14:11:13Z"   (the default tooltip)
        temp = dates[i].title;
        temp = temp.substring(0, temp.length - 1);
        temp += ' +0000';

        var format0 = ('YYYY-MM-DD HH:mm:ss Z');

        if (moment(temp, format0, true).isValid()) {

            dates[i].title = toTimeZone(temp, localTimezone);


            // 2015-12-08 10:38:53
            // Dec 8 at 10:38

            var regex2 = /(.*at\ ).*/;
            if (dates[i].innerHTML.match(regex2)) {

                var title = dates[i].title;
                var month = title.substring(8, 10);   // the '8'
                var time = title.substring(11, 16);   // the '10:38'

                if (month.substring(0, 1) === '0') {  // if the 1st digit in month is '0' then dont' use it in the replacing of datetext
                    month = month.substring(1, 2);
                }

                var datetext = dates[i].innerHTML;
                var dateTextArray = datetext.split(' ');
                dateTextArray[1] = month;
                dateTextArray[3] = time;

                dates[i].innerHTML = dateTextArray.join(' ');

            }


        }
    }

}



convertTitleDates();



new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
        console.log(mutation.type);
        console.log('CHANGE');
        convertTitleDates();
    });
}).observe(
    document.querySelector('#mainbar'), {           // monitor #mainbar for changes
        // attributes: true,
        childList: true,
        // characterData: true,
    }
);



new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
        console.log(mutation.type);
        console.log('CHANGE');
        convertTitleDates();
    });
}).observe(
    document.querySelector('#questions'), {         // monitor #questions for changes
        // attributes: true,
        childList: true,
        // characterData: true,
    }
);