Team:KULeuven/Tools/New Day/Date Retriever
From 2008.igem.org
(Difference between revisions)
m |
m |
||
(87 intermediate revisions not shown) | |||
Line 1: | Line 1: | ||
<html> | <html> | ||
<script type="text/javascript"> | <script type="text/javascript"> | ||
- | + | /* | |
- | + | * @author KULeuven | |
- | var | + | */ |
+ | var monthNames = new Array('January', 'February', 'March', 'April', 'May', | ||
'June', 'July', 'August', 'September', 'October', 'November', 'December'); | 'June', 'July', 'August', 'September', 'October', 'November', 'December'); | ||
+ | |||
+ | document.write(pageDate().toString().substring(0,11)); | ||
/* Extracts the corresponding calendar date out of the page url */ | /* Extracts the corresponding calendar date out of the page url */ | ||
Line 19: | Line 22: | ||
var dateTokens = wikiFirst.split("_"); | var dateTokens = wikiFirst.split("_"); | ||
- | var day = dateTokens[0]; | + | var day = parseInt(dateTokens[0]); |
- | var month | + | var month; |
- | var year = dateTokens[2]; | + | for (x in monthNames) { |
+ | if(dateTokens[1] == monthNames[x]) {month = x; break;} | ||
+ | } | ||
+ | var year = parseInt(dateTokens[2]); | ||
// Construct a Date object and return the result | // Construct a Date object and return the result | ||
var date = new Date(); | var date = new Date(); | ||
date.setFullYear(year, month, day); | date.setFullYear(year, month, day); | ||
+ | |||
return date; | return date; | ||
} | } | ||
Line 32: | Line 39: | ||
function datePast(ndays) { | function datePast(ndays) { | ||
var pastDate = new Date(); | var pastDate = new Date(); | ||
- | + | // getDate, setDate methods are rubbish, stick to numeric substraction! | |
- | return | + | var ntime = pageDate().getTime() - ndays*86400*1000; |
+ | pastDate.setTime(ntime); | ||
+ | |||
+ | return pastDate; | ||
} | } | ||
Line 39: | Line 49: | ||
function dateFuture(ndays) { | function dateFuture(ndays) { | ||
var futureDate = new Date(); | var futureDate = new Date(); | ||
- | + | // getDate, setDate methods are rubbish, stick to numeric adding! | |
- | return | + | var ntime = pageDate().getTime() + ndays*86400*1000; |
+ | futureDate.setTime(ntime); | ||
+ | |||
+ | return futureDate; | ||
} | } | ||
/* Returns the date of Friday previous week */ | /* Returns the date of Friday previous week */ | ||
function previousWeekFriday() { | function previousWeekFriday() { | ||
- | var dayOfWeek = pageDate.getDay(); | + | var dayOfWeek = pageDate().getDay(); |
- | var previousWeekFriday; | + | var previousWeekFriday = new Date(); |
switch(dayOfWeek) { | switch(dayOfWeek) { | ||
- | case 0: previousWeekFriday | + | // case 0 is Sunday, substract 9 days |
- | default: previousWeekFriday | + | case 0: previousWeekFriday = datePast(9); break; |
+ | default: previousWeekFriday = datePast(dayOfWeek + 2); break; | ||
} | } | ||
Line 58: | Line 72: | ||
/* Returns the date of Monday next week */ | /* Returns the date of Monday next week */ | ||
function nextWeekMonday() { | function nextWeekMonday() { | ||
- | var dayOfWeek = pageDate.getDay(); | + | var dayOfWeek = pageDate().getDay(); |
- | var nextWeekMonday; | + | var nextWeekMonday = new Date(); |
switch(dayOfWeek) { | switch(dayOfWeek) { | ||
- | case 0: nextWeekMonday | + | // case 0 is Sunday, add 1 day |
- | default: nextWeekMonday | + | case 0: nextWeekMonday = dateFuture(1); break; |
+ | default: nextWeekMonday = dateFuture(8 - dayOfWeek); break; | ||
} | } | ||
Line 71: | Line 86: | ||
/* Returns the date of Friday this week */ | /* Returns the date of Friday this week */ | ||
function thisFriday() { | function thisFriday() { | ||
- | var dayOfWeek = pageDate.getDay(); | + | var dayOfWeek = pageDate().getDay(); |
var thisFriday; | var thisFriday; | ||
switch(dayOfWeek) { | switch(dayOfWeek) { | ||
- | case 0: thisFriday | + | // case 0 is Sunday, substract 2 days |
- | case 6: thisFriday | + | case 0: thisFriday = datePast(2); break; |
- | default: thisFriday | + | // 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; | ||
} | } | ||
Line 85: | Line 102: | ||
/* Converts a date into a valid url for the KULeuven wiki and loads the page */ | /* Converts a date into a valid url for the KULeuven wiki and loads the page */ | ||
function goToDate(date) { | function goToDate(date) { | ||
+ | // base wiki url of KULeuven | ||
var URL = "https://2008.igem.org/Team:KULeuven/"; | var URL = "https://2008.igem.org/Team:KULeuven/"; | ||
URL += date.getDate(); | URL += date.getDate(); | ||
URL += '_'; | URL += '_'; | ||
+ | // month indexing of Date object index matching to array monthNames indexing | ||
URL += monthNames[date.getMonth()]; | URL += monthNames[date.getMonth()]; | ||
URL += '_'; | URL += '_'; | ||
URL += date.getFullYear(); | URL += date.getFullYear(); | ||
+ | // redirect browser to desired day | ||
window.location.href = URL; | window.location.href = URL; | ||
- | |||
- | |||
- | |||
- | |||
} | } | ||
</script> | </script> |
Latest revision as of 23:45, 29 October 2008
<< return to notebook | return to homepage >> | ||
< previous friday | ← yesterday | tomorrow → | next monday > |