Click to See Complete Forum and Search --> : Day of Year Script


Ringside
10-01-2005, 04:26 PM
I have searched high and low and the best I can find is this script that reveals the days until the end of the year. I have attempted mods to this script to revert the object to show it subtracted from 365 which gives a negative solution which is correct then getting the inverse to an absolute doesn't work.

Help script a day of the year. Many thanks.

<script>
today=new Date()
var endofyear=new Date(today.getFullYear(), 11, 31)
var one_day=1000*60*60*24
document.write(""+Math.ceil((endofyear.getTime()-today.getTime())/(one_day)-365)+" days remaining this year!")
</script>

Charles
10-01-2005, 04:40 PM
Days don't all have the same length so you have to use a different method to loop through adding one at a time:<script type="text/javascript">
Date.prototype.getYearDay = function () {
var n = 1;
var d = new Date (this.getFullYear(), 0, 1)
while (d.getTime() < this.getTime()) {d.setDate(d.getDate() + 1); n++}
return n
}

alert (new Date().getYearDay())
</script>

Ringside
10-01-2005, 04:48 PM
Many thanks. I had also found a relative count up script that utilized a set date and coverted in days but this one is far better since it is free of pre-set time.

Thanks.