Difference between revisions of "MediaWiki:Common.js"

From PathfinderWiki
Jump to: navigation, search
 
(62 intermediate revisions by the same user not shown)
Line 3: Line 3:
  
 
   var myElement = document.getElementById('jsexample');
 
   var myElement = document.getElementById('jsexample');
   myElement.innerHTML = "Test<table><tr><td>Tag: </td><td><input type='text' id='day'></td> </tr></table>";
+
 
 +
 
 +
        function daysInYear(year) {
 +
    if(year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) {
 +
        // Leap year
 +
        return 366;
 +
    } else {
 +
        // Not a leap year
 +
        return 365;
 +
    }
 +
}
 +
 
 +
function dayOfYear(dn, mn, yn) {   // d is a Date object
 +
var d1 = new Date(yn,0,1,12,0,0); // noon on Jan. 1
 +
var d2 = new Date(yn,mn-1,dn,12,0,0); // noon on input date
 +
var ddiff = Math.round((d2-d1)/864e5);
 +
return ddiff+1; }
 +
 
 +
     
 +
        function calculate_in ()
 +
        {
 +
          var day = day_in.value;
 +
          var month = month_in.value;
 +
          var year = year_in.value;
 +
          var hour = hour_in.value;
 +
          var minute = minute_in.value;
 +
 
 +
          var ydays = daysInYear(year);
 +
          var aday = dayOfYear(day, month, year);
 +
                   
 +
          var tmp = ((1/ydays * ( aday - 1 + hour/24 + minute/1440)) - 2323 + year/1);
 +
          var stardate = 1000 * ( year/1 + (1/ydays * ( aday - 1 + hour/24 + minute/1440)) -2323 );
 +
       
 +
          stardate_out.innerHTML = stardate;
 +
        }
 +
 
 +
        function calculate_out ()
 +
        {
 +
          var stardate =sdin.value;
 +
 
 +
         
 +
          var tmp = stardate/1000 + 2323;
 +
          var year = Math.floor(tmp);
 +
         
 +
          var ydays = daysInYear(year);
 +
 
 +
          var tmpd = (tmp - year) * ydays + 1;
 +
          var aday = Math.floor(tmpd);
 +
         
 +
  var d1 = new Date(year,0,1,12,0,0); // noon on Jan. 1
 +
  var d2 = (aday - 1)*864e5 + d1.getTime();
 +
  var mydate = new Date(d2);
 +
 
 +
          var day = mydate.getDate();
 +
          var month = mydate.getMonth() + 1;
 +
         
 +
         
 +
          var tmph = (tmpd - aday) * 24;
 +
          var hour = Math.floor(tmph);
 +
         
 +
          var tmpz = (tmph - hour) * 60;
 +
          var minute = Math.floor(tmpz);
 +
 
 +
          day_out.innerHTML= day;
 +
          month_out.innerHTML= month;
 +
          year_out.innerHTML= year;
 +
          hour_out.innerHTML= hour;
 +
          minute_out.value= minute;
 +
 
 +
        }
 +
 
 +
var out=document.createElement("p");
 +
myElement.appendChild(out);
 +
out.innerHTML="Tag: ";
 +
 
 +
var day_in= document.createElement("input");
 +
day_in.type = "text";
 +
out.appendChild(day_in);
 +
 
 +
var out=document.createElement("p");
 +
myElement.appendChild(out);
 +
out.innerHTML="Monat: ";
 +
 
 +
var month_in= document.createElement("input");
 +
month_in.type = "text";
 +
out.appendChild(month_in);
 +
 
 +
var out=document.createElement("p");
 +
myElement.appendChild(out);
 +
out.innerHTML="Jahr: ";
 +
 
 +
var year_in= document.createElement("input");
 +
year_in.type = "text";
 +
out.appendChild(year_in);
 +
 
 +
var out=document.createElement("p");
 +
myElement.appendChild(out);
 +
out.innerHTML="Stunde: ";
 +
 
 +
var hour_in= document.createElement("input");
 +
hour_in.type = "text";
 +
out.appendChild(hour_in);
 +
 
 +
var out=document.createElement("p");
 +
myElement.appendChild(out);
 +
out.innerHTML="Minute: ";
 +
 
 +
var minute_in= document.createElement("input");
 +
minute_in.type = "text";
 +
minute_in.setAttribute('onkeyup', 'calculate_in()');
 +
minute_in.onkeyup = function() { calculate_in(); };
 +
out.appendChild(minute_in);
 +
 
 +
out=document.createElement("p");
 +
myElement.appendChild(out);
 +
out.innerHTML="Sternzeit: ";
 +
 
 +
var stardate_out=document.createElement("output");
 +
out.appendChild(stardate_out);
 +
 
 +
 
 +
out=document.createElement("p");
 +
myElement.appendChild(out);
 +
out.innerHTML="Sternzeit: ";
 +
 
 +
var sdin= document.createElement("input");
 +
sdin.type = "text";
 +
sdin.setAttribute('onkeyup', 'calculate_out()');
 +
sdin.onkeyup = function() { calculate_out(); };
 +
out.appendChild(sdin);
 +
 
 +
out=document.createElement("p");
 +
myElement.appendChild(out);
 +
out.innerHTML="Tag: ";
 +
 
 +
var day_out=document.createElement("output");
 +
out.appendChild(day_out);
 +
 
 +
out=document.createElement("p");
 +
myElement.appendChild(out);
 +
out.innerHTML="Monat: ";
 +
 
 +
var month_out=document.createElement("output");
 +
out.appendChild(month_out);
 +
 
 +
out=document.createElement("p");
 +
myElement.appendChild(out);
 +
out.innerHTML="Jahr: ";
 +
 
 +
var year_out=document.createElement("output");
 +
out.appendChild(year_out);
 +
 
 +
out=document.createElement("p");
 +
myElement.appendChild(out);
 +
out.innerHTML="Stunde: ";
 +
 
 +
var hour_out=document.createElement("output");
 +
out.appendChild(hour_out);
 +
 
 +
out=document.createElement("p");
 +
myElement.appendChild(out);
 +
out.innerHTML="Minute: ";
 +
 
 +
var minute_out=document.createElement("output");
 +
out.appendChild(minute_out);
 
}());
 
}());

Latest revision as of 18:12, 3 September 2018

/* Any JavaScript here will be loaded for all users on every page load. */
$(function () {

  var myElement = document.getElementById('jsexample');


        function daysInYear(year) {
    if(year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) {
        // Leap year
        return 366;
    } else {
        // Not a leap year
        return 365;
    }
}

function dayOfYear(dn, mn, yn) {   // d is a Date object
var d1 = new Date(yn,0,1,12,0,0); // noon on Jan. 1
var d2 = new Date(yn,mn-1,dn,12,0,0); // noon on input date
var ddiff = Math.round((d2-d1)/864e5);
return ddiff+1; }

       
        function calculate_in ()
        {
          var day = day_in.value;
          var month = month_in.value;
          var year = year_in.value;
          var hour = hour_in.value;
          var minute = minute_in.value;

          var ydays = daysInYear(year);
          var aday = dayOfYear(day, month, year);
                    
          var tmp = ((1/ydays * ( aday - 1 + hour/24 + minute/1440)) - 2323 + year/1);
          var stardate = 1000 * ( year/1 + (1/ydays * ( aday - 1 + hour/24 + minute/1440)) -2323 );
         
          stardate_out.innerHTML = stardate;
        }

        function calculate_out ()
        {
          var stardate =sdin.value;

          
          var tmp = stardate/1000 + 2323;
          var year = Math.floor(tmp);
          
          var ydays = daysInYear(year);

          var tmpd = (tmp - year) * ydays + 1;
          var aday = Math.floor(tmpd);
          
	  var d1 = new Date(year,0,1,12,0,0); // noon on Jan. 1
	  var d2 = (aday - 1)*864e5 + d1.getTime();
	  var mydate = new Date(d2);

          var day = mydate.getDate();
          var month = mydate.getMonth() + 1;
          
          
          var tmph = (tmpd - aday) * 24;
          var hour = Math.floor(tmph);
          
          var tmpz = (tmph - hour) * 60;
          var minute = Math.floor(tmpz);

          day_out.innerHTML= day;
          month_out.innerHTML= month;
          year_out.innerHTML= year;
          hour_out.innerHTML= hour;
          minute_out.value= minute;

        }

var out=document.createElement("p");
myElement.appendChild(out);
out.innerHTML="Tag: ";

var day_in= document.createElement("input");
day_in.type = "text";
out.appendChild(day_in);

var out=document.createElement("p");
myElement.appendChild(out);
out.innerHTML="Monat: ";

var month_in= document.createElement("input");
month_in.type = "text";
out.appendChild(month_in);

var out=document.createElement("p");
myElement.appendChild(out);
out.innerHTML="Jahr: ";

var year_in= document.createElement("input");
year_in.type = "text";
out.appendChild(year_in);

var out=document.createElement("p");
myElement.appendChild(out);
out.innerHTML="Stunde: ";

var hour_in= document.createElement("input");
hour_in.type = "text";
out.appendChild(hour_in);

var out=document.createElement("p");
myElement.appendChild(out);
out.innerHTML="Minute: ";

var minute_in= document.createElement("input");
minute_in.type = "text";
minute_in.setAttribute('onkeyup', 'calculate_in()');
minute_in.onkeyup = function() { calculate_in(); };
out.appendChild(minute_in);

out=document.createElement("p");
myElement.appendChild(out);
out.innerHTML="Sternzeit: ";

var stardate_out=document.createElement("output");
out.appendChild(stardate_out);


out=document.createElement("p");
myElement.appendChild(out);
out.innerHTML="Sternzeit: ";

var sdin= document.createElement("input");
sdin.type = "text";
sdin.setAttribute('onkeyup', 'calculate_out()');
sdin.onkeyup = function() { calculate_out(); };
out.appendChild(sdin);

out=document.createElement("p");
myElement.appendChild(out);
out.innerHTML="Tag: ";

var day_out=document.createElement("output");
out.appendChild(day_out);

out=document.createElement("p");
myElement.appendChild(out);
out.innerHTML="Monat: ";

var month_out=document.createElement("output");
out.appendChild(month_out);

out=document.createElement("p");
myElement.appendChild(out);
out.innerHTML="Jahr: ";

var year_out=document.createElement("output");
out.appendChild(year_out);

out=document.createElement("p");
myElement.appendChild(out);
out.innerHTML="Stunde: ";

var hour_out=document.createElement("output");
out.appendChild(hour_out);

out=document.createElement("p");
myElement.appendChild(out);
out.innerHTML="Minute: ";

var minute_out=document.createElement("output");
out.appendChild(minute_out);
}());