Langbahn Team – Weltmeisterschaft

User:Genius101/time.js

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
$(function()
{
	if (wgCanonicalNamespace == '' || wgCanonicalNamespace == 'MediaWiki' || wgCanonicalNamespace == 'Special')
		return;
 
	var disabled_urls = new Array('action=history');
	for (var i = 0; i < disabled_urls.length; i++)
	{
		if (document.location.href.indexOf(disabled_urls[i]) != -1)
			return;
	}
 
	var unique_url = false;
 
	var wikiPreview = new Array('action=edit', 'action=submit');
	for (var i = 0; i < wikiPreview.length; i++)
	{
		if (document.location.href.indexOf(wikiPreview[i]) != -1)
			unique_url = 'wikiPreview';
	}
 
	var element_id = unique_url ? unique_url : 'bodyContent';
	document.getElementById(element_id).innerHTML = 
		document.getElementById(element_id).innerHTML.replace(/(\d\d):(\d\d), (\d{1,2}) ([A-Z][a-z]+) (\d{4}) \(UTC\)/g, adjustTime);
});
 
function add_leading_zero(number)
{
	if (number < 10)
		number = "0" + number;
	return number;
}
 
function adjustTime(str, old_hour, old_minute, old_day, old_month, old_year, offset, s)
{
	var today = new Date();
	var yesterday = new Date();
	yesterday.setDate(yesterday.getDate() - 1);
 
	// set the date entered
	var time = new Date();
	time.setUTCHours(old_hour);
	time.setUTCMinutes(old_minute);
	// fix for when the date is the 31st of the month
	if (old_day == '31')
		time.setUTCDate('30');
	else
		time.setUTCDate(old_day);
	time.setUTCMonth(convert_month_to_number(old_month));
	time.setUTCFullYear(old_year);
 
	// figure out what the time offset is
	var utc_offset = -1 * time.getTimezoneOffset() / 60;
	if (utc_offset >= 0)
		utc_offset = '+' + utc_offset;
 
	// set the date bits to output
	var year = time.getFullYear();
	var month = add_leading_zero(time.getMonth() + 1);
	// fix for when the date is the 31st of the month
	if (old_day == '31')
		var day = time.getDate() + 1;
	else
		var day = time.getDate();
	var hour = add_leading_zero(parseInt(time.getHours()));
	var minute = add_leading_zero(time.getMinutes());
 
	// output am or pm depending on the date
	var ampm = 'am';
	if (hour > 11)
		ampm = 'pm';
	if (hour > 12)
		hour -= 12;
	if (hour == '00')
		hour = 12;
 
	// Return 'today' or 'yesterday' if that is the case
	if (year == today.getFullYear() && month == add_leading_zero(today.getMonth() + 1) && day == today.getDate())
		var date = 'Today';
	else if (year == yesterday.getFullYear() && month == add_leading_zero(yesterday.getMonth() + 1) && day == yesterday.getDate())
		var date = 'Yesterday';
	else
	{
		// Calculate day of week
		var day_names = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
		var day_of_the_week = day_names[time.getDay()];
 
		// The distance in days from today and last Monday?
		today = new Date(today.getYear(), today.getMonth(), today.getDate());
		time = new Date(time.getYear(), time.getMonth(), time.getDate());
 
		// Calculate time difference
		var milliseconds_ago = today.getTime() - time.getTime();
		var days_ago = milliseconds_ago / 1000 / 60 / 60 / 24;
		days_ago = Math.round(days_ago);
 
		if (days_ago <= 7)
			var last = 'last ';
		else
			var last = '';
 
		// The difference between today and the timestamp
		var difference, difference_word = '';
		if (today.valueOf() >= time.valueOf())
		{
			difference = new Date(today.valueOf() - time.valueOf());
			difference_word = 'ago';
		}
		else
		{
			difference = new Date(time.valueOf() - today.valueOf());
			difference_word = 'from now';
		}
		var descriptive_difference = [];
 
		if (difference.getYear() - 70 > 0)
		{
			var years_ago = (difference.getYear() - 70) + ' ' + 'year'.pluralize(difference.getYear() - 70, 'years');
			descriptive_difference.push(years_ago);
		}
		if (difference.getMonth() > 0)
		{
			var months_ago = difference.getMonth() + ' ' + 'month'.pluralize(difference.getMonth(), 'months');
			descriptive_difference.push(months_ago);
		}
		if (difference.getDate() > 0)
		{
			var days_ago = difference.getDate() + ' ' + 'day'.pluralize(difference.getDate(), 'days');
			descriptive_difference.push(days_ago);
		}
 
		var date = year + '-' + month + '-' + add_leading_zero(day) + ', ' + last + day_of_the_week + ' (' + descriptive_difference.join(', ') + ' ' + difference_word + ')';
	}
 
	var time = hour + ':' + minute + ' ' + ampm;
	var return_date = date + ', ' + time + ' (UTC' + utc_offset + ')';
	return "<span style='font-size: 90%;' title='" + str + "'>" + return_date.replace(/ /g, '&nbsp;') + '</span>';
}
 
function convert_month_to_number(month)
{
   var tmp = new Date(month + " 1, 2006");
   return tmp.getMonth();
}
 
String.prototype.pluralize = function(count, plural)
{
  if (plural == null)
    plural = this + 's';
 
  return (count == 1 ? this : plural)
}