Corrected code:
// document level script; // define dayOfYear property for the Date object; Date.prototype.getDayOfYear= function(){ var j1= new Date(this); j1.setMonth(0, 0, 0, 0, 0, 0); // set time to midnight; return Math.round((this-j1)/(1000 * 60 * 60 * 24)); // divide number of days by value of one day; } // end definition of dayOfYear property; // // end document level script
event.value = ""; // clear field var cMyDate = this.getField("MyDate").valueAsString; // get field value; if(cMyDate != ""){ // process only non-blank vaues. var oMyDate = util.scand("d-mmm-yyyyy", cMyDate); // convert to date object; event.value = oMyDate.getDayOfYear(); // get day of year from date object; }
Or one can count the number of days since January 1:
// document level funcions Date.prototype.isLeapYear = function() { // return ture (1) if year is a leap year; var year = this.getFullYear(); if((year & 3) != 0) return false; return ((year % 100) != 0 || (year % 400) == 0); };
// Get Day of Year Date.prototype.getDOY = function() { var dayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]; // accumulated number of days before month; var mn = this.getMonth(); // month fromthe date object; var dn = this.getDate(); // date from date object; var dayOfYear = dayCount[mn] + dn; // number of days for prior months and days of month; if(mn > 1 && this.isLeapYear()) dayOfYear++; // adjust for leap year (February 29th); return dayOfYear; }; // // end document level funcitons;
event.value = ""; // clear field var cMyDate = this.getField("MyDate").valueAsString; // get value of input date; if(cMyDate != ""){ // process only if value is non-blank; var oMyDate = util.scand("d-mmm-yyyyy", cMyDate); // convert input date string to date object; event.value = oMyDate.getDOY(); // count the number of days from the start of the year; }
... View more