These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

using the day of the year for Auto ID

long3773
Registered: Jan 16 2009
Posts: 5
Answered

HI Experts,

I am very new to javascript and in need to create one of a form field to be something like Auto ID using the day of the year for example: "0203 " for January 20, 2003, this consists of Day Day Day Year or something like that.

Any assistance or direction will be appreciated. Thank you.

Long

Product: AcroForm

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Have you looked at the JavaScript date time object?

In JavaScript this object can be converted into a number that will be number of milliseonds form a set Epoch date. Once you get this value of January 1 and the current date, a subtraction and a division will get you there.

George Kaiser

long3773
Registered: Jan 16 2009
Posts: 5
Hi George,

Thanks for the quick response, this is what I have done so far, but I can't get the calculation of the first day of the year to work George which means every year I have to open the code and revise the date in the "StartDate" field. This doesn't sound right at all. Another thing is that how we could we have the output for three (3) digits instead of 2 with a leading zero?. For example: 019 vs 19?

// get the start date and convert to date object milliseconds
var date1 = util.scand("mm/dd/yy", this.getField("StartDate").value);
// get the end date and convert to date object milliseconds
var date2 = util.scand("mm/dd/yy", this.getField("Today").value);
// compute difference in milliseconds
event .value=(date2-date1)/1000/60/60/24 +1;

Is there better way to handle it?

Thanks so much for your help.

Long
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
There is, if you can trust that the user's computer is set to the correct date and time for their location.

The JavaScript constrictor "Date()" can provide the date time object or create a date time object. Without parameters the Date constrictor provides the current date time object. If one provides parameters for the full year, zero based month and date one can obtain the date time object for a given date. The use of other parameters allows the creation of a date time object from other values like milliseconds or certain date strings.

// some time constants based on millisecondsvar fSec = 1000; // milliseconds in a secondvar fMin = fSec * 60; // milliseconds in a minutevar fHr = fMin * 60; // number of milliseconds in an hourvar fDay = fHr * 24; // number of milliseconds in a day//// get the JavaScript DateTime object for nowvar oNow = new Date();// split some data from the objectvar sFullYear = oNow.getFullYear(); // get 4 digit yearvar sMonth = oNow.getMonth(); // get zero based month: January = 0var sDate = oNow.getDate(); // get date of monthvar sDay = oNow.getDay(); // get zero based day of week: Sunday = 0var sTimeZoneOffset = oNow.getTimezoneOffset(); // get the local time zone offset in minutes//// display some the valuesconsole.println('oNow = ' + oNow);console.println('Year = ' + sFullYear);console.println('Month = ' + sMonth);console.println('Date = ' + sDate);console.println('Day = ' + sDay);console.println('Local Time Zone Offset in minutes = ' + sTimezoneOffset);console.println('Local Time Zone Offset in hours = ' + (sTimezoneOffset / 60));// convert Now to start of dayoToday = new Date(sFullYear, sMonth, sDate);console.println('Start of today: ' + oToday);//// create date time object for Jan 1 of current year from year, month and dayvar oNewYear = new Date(sFullYear, 0, 1);console.println('Jan 1 for this year: ' + oNewYear);//// OK let's now do the mathvar fNewYear = oNewYear.valueOf(); // get milliseconds since Epoch for start of yearvar fToday = oToday.valueOf(); // get milliseconds since Epoch for todayvar fDiff = fToday - fNewYear; // get difference between the dates in millisecondsvar fDayYear = fDiff / fDay; // convert millisecond of year to day of year: zero basedfDayYear = fDayYear + 1; // adjust to non-zero day baseconsole.println('Day of Year for today: ' + fDayYear);

George Kaiser

long3773
Registered: Jan 16 2009
Posts: 5
Hi Gkaiseril,

Very complete and informative.

Thank you very, very much. I truly appreciate your help.

Long