Team:KULeuven/Tools/New Day/Date Retriever

From 2008.igem.org

(Difference between revisions)
m
Line 1: Line 1:
<html>
<html>
<script type="text/javascript">
<script type="text/javascript">
 +
 +
var pageDate;
var mnames = new Array('January', 'February', 'March', 'April', 'May',
var mnames = new Array('January', 'February', 'March', 'April', 'May',
'June', 'July', 'August', 'September', 'October', 'November', 'December');
'June', 'July', 'August', 'September', 'October', 'November', 'December');
Line 6: Line 8:
var dnames = new Array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
var dnames = new Array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
-
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_last_token = url_tokens[url_tokens.length-1];
+
var urlLast = urlTokens[urlTokens.length - 1];
-
var wiki_tokens = url_last_token.split("#");
+
// Retrieve page date
-
var url_date_token = wiki_tokens[0];
+
var wikiTokens = urlLast.split("#");
 +
var wikiFirst = wikiTokens[0];
-
var date_tokens = url_date_token.split("_");
+
// Retrieve seperate date components
-
var day = date_tokens[0];
+
var dateTokens = wikiFirst.split("_");
-
var month = date_tokens[1];
+
-
var year = date_tokens[2];
+
-
// Date is retrieved expressed in milliseconds
+
-
return Date.parse(day + ' ' + month + ', ' + year);
+
-
}
+
-
// arg determines whether you take yesterday (arg = 0) or tomorrow (arg = 1)
+
var day = dateTokens[0];
-
function date(arg) {
+
var month = dateTokens[1];
-
var numeric_date = date_retriever();
+
var year = dateTokens[2];
-
switch(arg) {
+
-
case 0: numeric_date -= 86400*1000; break;
+
-
case 1: numeric_date += 86400*1000; break;
+
-
default: break;
+
-
}
+
-
var date = new Date();
+
// Construct a Date object and return the result
-
date.setTime(numeric_date);
+
return (new Date()).setFullYear(year, month, day);
 +
}
-
return date;
+
/* Retrieves date ndays before this page date */
 +
function datePast(ndays) {
 +
var pastDate = new Date();
 +
pastDate.setDate(pageDate.getDate() - days);
 +
return pageDate;
}
}
-
function date_next_week_monday() {
+
/* Searches date ndays after this page date */
-
var numeric_date = date_retriever();
+
function dateFuture(ndays) {
 +
var futureDate = new Date();
 +
futureDate.setDate(pageDate.getDate() + days);
 +
return futureDays;
 +
}
-
var next_monday = new Date();
+
/* Returns the date of Friday previous week */
-
next_monday.setTime(numeric_date);
+
function previousWeekFriday() {
 +
var dayOfWeek = pageDate.getDay();
 +
var previousWeekFriday;
-
var day_of_week = next_monday.getDay();
+
switch(dayOfWeek) {
-
switch(day_of_week) {
+
case 0: previousWeekFriday.setDate(pageDate.getDate() - 9); break;
-
// Sunday is indexed as 0
+
default: previousWeekFriday.setDate(pageDate.getDate() - (dayOfWeek + 2));
-
case 0: numeric_date += 86400*1000; 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;
-
var previous_friday = new Date();
+
switch(dayOfWeek) {
-
previous_friday.setTime(numeric_date);
+
case 0: nextWeekMonday.setDate(pageDate.getDate() + 1); break;
-
 
+
default: nextWeekMonday.setDate(pageDate.getDate() + (8 - dayOfWeek));
-
var day_of_week = previous_friday.getDay();
+
-
switch(day_of_week) {
+
-
// Sunday is indexed as 0
+
-
case 6: numeric_date -= 86400*1000; break;
+
-
default: numeric_date -= (2 + day_of_week)*86400*1000; break;
+
}
}
-
previous_friday.setTime(numeric_date);
+
return nextWeekMonday;
-
 
+
-
return previous_friday;
+
}
}
-
function url_date(date) {
+
/* Returns the date of Friday this week */
-
var url = "https://2008.igem.org/Team:KULeuven/";
+
function thisFriday() {
-
url += date.getDate();
+
var dayOfWeek = pageDate.getDay();
-
url += '_';
+
var thisFriday;
-
url += mnames[date.getMonth()];
+
 
-
url += '_';
+
switch(dayOfWeek) {
-
url += date.getFullYear();
+
case 0: thisFriday.setDate(pageDate.getDate() - 2); break;
 +
case 6: thisFriday.setDate(pageDate.getDate() - 1); break;
 +
default: thisFriday.setDate(pageDate.getDate() + (5 - dayOfWeek));
 +
}
 +
 
 +
return thisFriday;
 +
}
-
window.location.href=url;
+
/* Converts a date into a valid url for the KULeuven wiki and loads the page */
 +
function goToDate(date) {
 +
var URL = "https://2008.igem.org/Team:KULeuven/";
 +
URL += date.getDate();
 +
URL += '_';
 +
URL += monthNames[date.getMonth()];
 +
URL += '_';
 +
URL += date.getFullYear();
 +
window.location.href = URL;
}
}
</script>
</script>
Line 95: Line 104:
</tr>
</tr>
<tr align="center">
<tr align="center">
-
<td align="left" width="25%"><a href="javascript:void(0);" onClick="url_date(date_previous_week_friday());">&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%"><a href="javascript:void(0);" onClick="url_date(date(0));">← yesterday</a></td>
+
<td width="25%"><a href="javascript:void(0);" onClick="goToDate(datePast(1));">← yesterday</a></td>
-
<td width="25%"><a href="javascript:void(0);" onClick="url_date(date(1));">tomorrow →</a></td>
+
<td width="25%"><a href="javascript:void(0);" onClick="goToDate(dateFuture(1));">tomorrow →</a></td>
-
<td align="right" width="25%"><a href="javascript:void(0);" onClick="url_date(date_next_week_monday());">next monday &gt;</a></td>
+
<td align="right" width="25%"><a href="javascript:void(0);" onClick="goToDate(nextWeekMonday());">next monday &gt;</a></td>
</tr>
</tr>
</table>
</table>

Revision as of 09:11, 31 July 2008

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