Click to See Complete Forum and Search --> : julian date/ days gone by issues


hoonak
01-31-2003, 02:02 PM
Greetings,
I am working on a piece of code that totals how many days have passed so far this year. This is where I am at and I am close, but I cannot see my error:
var month = new Array(12);
month [0] = 31; //jan31
month [1] = 28; //feb28
month [2] = 31; //mar31
month [3] = 30; //apr30
month [4] = 31; //may31
month [5] = 30; //jun30
month [6] = 31; //jul31
month [7] = 31; //aug31
month [8] = 30; //sep30
month [9] = 31; //oct31
month [10] = 30; //nov30
month [11] = 31; //dec31


var currentDate = new Date(); //creates a date variable
monthValue = currentDate.getMonth(); //gets month in numeric format
dateValue = currentDate.getDate(); //gets date in numeric format

var ytd = 0; //year to date
function julianDate() {
for (var i=0; i<monthValue; i++) { // totals days of previous months
ytd += month[i] + dateValue; // adds days from previous months to today's date to get the julian date.
}
window.status = ytd; //
document.write(+ytd+ " days have passed so far this year.");
}

julianDate();
any and all assistance is appreciated,
thanx,
Mr Moose

numba_one
01-31-2003, 02:20 PM
I can't help you on the code above but i have a code that counts up from a set date. give it a try:


&<SCRIPT LANGUAGE="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
function countup(yr,m,d) {
var today=new Date();
var todayy=today.getYear();

// Y2K Fix by Isaac Powell
// http://onyx.idbsu.edu/~ipowell

if ((navigator.appName == "Microsoft Internet Explorer") && (todayy < 2000))
todayy="19" + todayy;
if (navigator.appName == "Netscape")
todayy=1900 + todayy;

var todaym=today.getMonth();
var todayd=today.getDate();
var todaystring=montharray[todaym]+" "+todayd+", "+todayy;
var paststring=montharray[m-1]+" "+d+", "+yr;
var difference=(Math.round((Date.parse(todaystring)-Date.parse(paststring))/(24*60*60*1000))*1);

document.write("There has been" + difference + " days since the start of 2003.");

}
countup(2003,01,01); // Date in format: (year,month,day)
// End -->
</script>

gil davis
01-31-2003, 04:33 PM
<script>
var now = new Date();
var then = new Date(now.getFullYear(),0,1);
var elapsed = now - then;
var ndys = Math.floor(elapsed / (1000 * 60 * 60 * 24));
alert(ndys);
</script>

hoonak
01-31-2003, 06:21 PM
for the record:
gil davis rules all!

Stan
08-11-2003, 01:26 AM
gil davis,

Your code of:

script>
var now = new Date();
var then = new Date(now.getFullYear(),0,1);
var elapsed = now - then;
var ndys = Math.floor(elapsed / (1000 * 60 * 60 * 24));
alert(ndys);
</script>

when displayed on my system shows the day being 221, when an MS Access database and a printed julian date cross reference shows the 11th of Aug to be 223 day.

I could really use this code for a web site I'm updating.

Thanks,

Stan

gil davis
08-11-2003, 07:32 AM
It calculates elapsed days, not the Julian day. I could see adding one to get the Julian day (ordinal number). The time of day might be a problem since it uses Math.floor(). The formula basically uses the integer (Math.floor) of "now" minus [midnight of Jan 1 current year].

Sanity check:
31 + 28 + 31 + 30 + 31 + 30 + 31 + 11 = 223 <== ok, I agree

So, instead of Math.floor(), use Math.round() and then add 1 to get the Julian day.

Stan
08-11-2003, 07:30 PM
Understood and thanks