Forum Moderators: open

Message Too Old, No Replies

Calculate up from a particular year?

Work up number of years from year in last century?

         

1Lit

2:29 am on May 27, 2002 (gmt 0)

10+ Year Member



I'd like to automatically display the year of various film festivals on my movie site e.g. 55th Cannes Film Festival this year, 56th next year.

I've spent an hour looking for a javascript to calculate up from a particular year (1947 in the case of Cannes). There are various sites with scripts to calculate the number of days you have been alive etc., but not a single one for the number of years up from a particular year.

I found a script which calculates the number of days from a particular date (below). I was wondering if somebody could possibly convert it to count up from a year.


<SCRIPT Language="JavaScript">
<!-- hide from old browsers
var today = new Date()
var targetDate = new Date("12/12/2001") //use full year
var timeAfterTarget = Math.floor(( today.getTime()
- targetDate.getTime() ) / 86400000)
var msg = "Days up from specified date " + timeAfterTarget + "."
document.write(msg)
//-->
</SCRIPT>

One other thing: is it possible to incorporate this feature [javascript.internet.com] into the resultant year, so that it'll display the correct ending after the year, i.e. 51st, 52nd, 53rd.

Thank you VERY much. This will help me out big time :)

DrDoc

6:45 am on May 27, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, since you only need the year, here's a script that will work. :)

<script language="javascript" type="text/javascript">
var start = 1947;
var now = new Date();
diff = (now.getYear() > 1900 ? now.getYear() : (now.getYear() + 100)) - start;
rx = /[^1]1$/;
if(rx.test(diff)){
diff += "st";
}
else{
rx = /[^1]2$/;
if(rx.test(diff)){
diff += "nd";
}
else{
rx = /[^1]3$/;
if(rx.test(diff)){
diff += "rd";
}
else{
diff += "th";
}
}
}
document.write(diff);
</script>