Team:KULeuven/Tools/New Day/Date Retriever

From 2008.igem.org

(Difference between revisions)
m
m
 
(105 intermediate revisions not shown)
Line 1: Line 1:
<html>
<html>
<script type="text/javascript">
<script type="text/javascript">
-
var mnames = new Array('January', 'February', 'March', 'April', 'May',
+
/*
 +
* @author KULeuven
 +
*/
 +
var monthNames = new Array('January', 'February', 'March', 'April', 'May',
'June', 'July', 'August', 'September', 'October', 'November', 'December');
'June', 'July', 'August', 'September', 'October', 'November', 'December');
-
var dnames = new Array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
+
document.write(pageDate().toString().substring(0,11));
-
function date_retriever() {
+
/* Extracts the corresponding calendar date out of the page url */
-
// First step is to split the url of the current notebook entry
+
function pageDate() {
-
var url_tokens = location.href.split("/");
+
// Retrieve page url
-
// Filter out the last token which contains the proper date
+
var urlTokens = location.href.split("/");
-
var url_date_token = url_tokens[url_tokens.length-1];
+
var urlLast = urlTokens[urlTokens.length - 1];
-
var date_tokens = url_date_token.split("_");
+
// Retrieve page date
-
var day = date_tokens[0];
+
var wikiTokens = urlLast.split("#");
-
var month = date_tokens[1];
+
var wikiFirst = wikiTokens[0];
-
var year = date_tokens[2];
+
 
-
// Date is retrieved expressed in milliseconds
+
// Retrieve seperate date components
-
return Date.parse(day + ' ' + month + ', ' + year);
+
var dateTokens = wikiFirst.split("_");
-
}
+
-
// arg determines whether you take yesterday (arg = 0) or tomorrow (arg = 1)
+
var day = parseInt(dateTokens[0]);
-
function date(arg) {
+
var month;
-
var numeric_date = date_retriever();
+
for (x in monthNames) {
-
switch(arg) {
+
if(dateTokens[1] == monthNames[x]) {month = x; break;}
-
case 0: numeric_date -= 86400*1000; break;
+
-
case 1: numeric_date += 86400*1000; break;
+
-
default: break;
+
}
}
 +
var year = parseInt(dateTokens[2]);
 +
// Construct a Date object and return the result
var date = new Date();
var date = new Date();
-
date.setTime(numeric_date);
+
date.setFullYear(year, month, day);
return date;
return date;
}
}
-
function url_date(date) {
+
/* Retrieves date ndays before this page date */
-
var url = "https://2008.igem.org/Team:KULeuven/";
+
function datePast(ndays) {
-
url += date.getDate();
+
var pastDate = new Date();
-
url += '_';
+
// getDate, setDate methods are rubbish, stick to numeric substraction!
-
url += mnames[date.getMonth()];
+
var ntime = pageDate().getTime() - ndays*86400*1000;
-
url += '_';
+
pastDate.setTime(ntime);
-
url += date.getFullYear();
+
-
return url;
+
return pastDate;
}
}
-
function date_next_week_monday() {
+
/* Searches date ndays after this page date */
-
var numeric_date = date_retriever();
+
function dateFuture(ndays) {
 +
var futureDate = new Date();
 +
// getDate, setDate methods are rubbish, stick to numeric adding!
 +
var ntime = pageDate().getTime() + ndays*86400*1000;
 +
futureDate.setTime(ntime);
-
var next_monday = new Date();
+
return futureDate;
-
next_monday.setTime(numeric_date);
+
}
 +
 
 +
/* Returns the date of Friday previous week */
 +
function previousWeekFriday() {
 +
var dayOfWeek = pageDate().getDay();
 +
var previousWeekFriday = new Date();
-
var day_of_week = next_monday.getDay();
+
switch(dayOfWeek) {
-
switch(day_of_week) {
+
// case 0 is Sunday, substract 9 days
-
// Sunday is indexed as 0
+
case 0: previousWeekFriday = datePast(9); break;
-
case 0: numeric_date += 86400*1000; break;
+
default: previousWeekFriday = datePast(dayOfWeek + 2); break;
-
default: numeric_date += (8-day_of_week)*86400*1000; break;
+
}
}
-
next_monday.setTime(numeric_date);
+
return previousWeekFriday;
-
 
+
-
return next_monday;
+
}
}
-
function date_previous_week_friday() {
+
/* Returns the date of Monday next week */
-
var numeric_date = date_retriever();
+
function nextWeekMonday() {
 +
var dayOfWeek = pageDate().getDay();
 +
var nextWeekMonday = new Date();
-
var previous_friday = new Date();
+
switch(dayOfWeek) {
-
previous_friday.setTime(numeric_date);
+
// case 0 is Sunday, add 1 day
 +
case 0: nextWeekMonday = dateFuture(1); break;
 +
default: nextWeekMonday = dateFuture(8 - dayOfWeek); break;
 +
}
-
var day_of_week = previous_friday.getDay();
+
return nextWeekMonday;
-
switch(day_of_week) {
+
}
-
// Sunday is indexed as 0
+
 
-
case 6: numeric_date -= 86400*1000; break;
+
/* Returns the date of Friday this week */
-
default: numeric_date -= (2 + day_of_week)*86400*1000; break;
+
function thisFriday() {
 +
var dayOfWeek = pageDate().getDay();
 +
var thisFriday;
 +
 
 +
switch(dayOfWeek) {
 +
// case 0 is Sunday, substract 2 days
 +
case 0: thisFriday = datePast(2); break;
 +
// case 6 is Saturday, substract 1 day, can also be merged into default, in which case we look -1 day // ahead, corresponding to going 1 day back in the past
 +
case 6: thisFriday = datePast(1); break;
 +
default: thisFriday = dateFuture(5 - dayOfWeek); break;
}
}
-
previous_friday.setTime(numeric_date);
+
return thisFriday;
 +
}
-
return previous_friday;
+
/* Converts a date into a valid url for the KULeuven wiki and loads the page */
 +
function goToDate(date) {
 +
// base wiki url of KULeuven
 +
var URL = "https://2008.igem.org/Team:KULeuven/";
 +
URL += date.getDate();
 +
URL += '_';
 +
// month indexing of Date object index matching to array monthNames indexing
 +
URL += monthNames[date.getMonth()];
 +
URL += '_';
 +
URL += date.getFullYear();
 +
// redirect browser to desired day
 +
window.location.href = URL;
}
}
</script>
</script>
Line 92: Line 124:
</tr>
</tr>
<tr align="center">
<tr align="center">
-
<td align="left" width="25%"><a href="javascript:url_date(date_previousweek_friday());">&lt;&lt; previous friday</a></td>
+
<td align="left" width="25%"><a href="javascript:void(0);" onClick="goToDate(previousWeekFriday());">&lt; previous friday</a></td>
-
<td width="25%"><script language="javascript">document.write("< yesterday".link(url_date(date(0))));</script></td>
+
<td width="25%"><a href="javascript:void(0);" onClick="goToDate(datePast(1));">← yesterday</a></td>
-
<td width="25%"><script language="javascript">document.write("tomorrow >".link(url_date(date(1))));</script></td>
+
<td width="25%"><a href="javascript:void(0);" onClick="goToDate(dateFuture(1));">tomorrow →</a></td>
-
<td align="right" width="25%"><script language="javascript">document.write("next monday >>".link(url_date(date_next_week_monday())));</script></td>
+
<td align="right" width="25%"><a href="javascript:void(0);" onClick="goToDate(nextWeekMonday());">next monday &gt;</a></td>
</tr>
</tr>
</table>
</table>

Latest revision as of 23:45, 29 October 2008

<< return to notebook return to homepage >>
< previous friday ← yesterday tomorrow → next monday >