• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Attempting to create a box that automatically displays the Julian date of this year.

New Here ,
Oct 25, 2019 Oct 25, 2019

Copy link to clipboard

Copied

I have a form that has a calendar that auto-populates a small calendar based on the selection of the first day.  I need to also display the Julian date (of that year) next to it.  Ideally, the code would read the first selected date, add the appropriate number of days to match the applicable calendar date, and then display the Julian date.  I have attached a picture for reference.  The Monday block is the manual selection and the rest update automatically based on the selection.  Any help would be greatly appreciated.

 

 

Calendar.PNG

Views

2.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 26, 2019 Oct 26, 2019

I tried the code above and got the following error message:

TypeError: oMyDate.getDayOfYear is not a function

I believe that not all versions of Acrobat support adding methods to the native objects (like Date), so if you're getting the same result try this code, instead:

 

// define getDayOfYear function
function getDayOfYear(d) {
	var j1= new Date(d);
	j1.setMonth(0, 0, 0, 0, 0, 0);
	return Math.round((d-j1)/(1000 * 60 * 60 * 24));
}

event.value = ""; // clear field
var cMyDate = this.getField
...

Votes

Translate

Translate
LEGEND ,
Oct 25, 2019 Oct 25, 2019

Copy link to clipboard

Copied

I don’t see an Julian dates?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 25, 2019 Oct 25, 2019

Copy link to clipboard

Copied

I already have the box there (read only, that's why it can't be seen to the right of the date) but am missing the code for it to auto-fill.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 25, 2019 Oct 25, 2019

Copy link to clipboard

Copied

But since there isn't an example we don't know what you mean by "Julian date". To me, years don't have Julian date, and today's Julian date is 2019.298. Some people write 19298. Some people use a different system where this instant is 2458782.17492. None of these are years.

 

Nobody is wrong, but we need to know what you need.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 25, 2019 Oct 25, 2019

Copy link to clipboard

Copied

Gotcha, sorry for the confusion.  I just need it to display the current day of the year (today is 298).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 25, 2019 Oct 25, 2019

Copy link to clipboard

Copied

One possible way is to define a new property for the Date object named "getDayOfYear"  and then use that property to get the day of the year from the date object of a date.

 

One can use the following code in the custom calculation script for the field with the result for the day of the year.

 

// define getDayOfYear property for the Date object;
Date.prototype.getDayOfYear= function(){
var j1= new Date(this);
j1.setMonth(0, 0, 0, 0, 0, 0);
return Math.round((this-j1)/(1000 * 60 * 60 * 24));
}
// end definition of getDayOfYear property;

event.value = ""; // clear field
var cMyDate = this.getField("MyDate").valueAsString;
if(cMyDate != ""){
var oMyDate = util.scand("d-mmm-yyyyy", cMyDate);
event.value = oMyDate.getDayOfYear();
}

 

You will need to adjust the field name for the input date and format of that string as necessary

If you wanted to you could place the code for the "getDayOfYear" as a document level script and then it would be available to all fields in the form. There are also other methods for computing the day of the year.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 25, 2019 Oct 25, 2019

Copy link to clipboard

Copied

Thank you so much for your help.  I pasted the script into the custom calculation script, adjust the "MyDate" field and adjusted the [var oMyDate] to the correct format and nothing is displaying in the box.  Is there something I'm missing?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 26, 2019 Oct 26, 2019

Copy link to clipboard

Copied

Check the JS Console (Ctrl+J) for error messages.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 26, 2019 Oct 26, 2019

Copy link to clipboard

Copied

I tried the code above and got the following error message:

TypeError: oMyDate.getDayOfYear is not a function

I believe that not all versions of Acrobat support adding methods to the native objects (like Date), so if you're getting the same result try this code, instead:

 

// define getDayOfYear function
function getDayOfYear(d) {
	var j1= new Date(d);
	j1.setMonth(0, 0, 0, 0, 0, 0);
	return Math.round((d-j1)/(1000 * 60 * 60 * 24));
}

event.value = ""; // clear field
var cMyDate = this.getField("MyDate").valueAsString;
if(cMyDate != ""){
	var oMyDate = util.scand("d-mmm-yyyyy", cMyDate);
	if (oMyDate!=null) event.value = getDayOfYear(oMyDate);
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 26, 2019 Oct 26, 2019

Copy link to clipboard

Copied

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;
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 26, 2019 Oct 26, 2019

Copy link to clipboard

Copied

That did it! Thank you so much for your help!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 27, 2019 Oct 27, 2019

Copy link to clipboard

Copied

A follow-up question, and I only noticed because we're close to the end of the year.  My source box is the only one that has a date that can be used as the source for the "DayOfYear" function.  As such, if my source date is before Jan 1, my result will be greater than 365 once it goes past Jan 1.  Below are a picture and the code I am using.  Is there any way to account for this in the code and add to the date before it calculates the day of the year?

 

// define getDayOfYear function
function getDayOfYear(d) {
var j1= new Date(d);
j1.setMonth(0, 0, 0, 0, 0, 0);
return Math.round((d-j1)/(1000 * 60 * 60 * 24));
}

event.value = ""; // clear field
var cMyDate = this.getField("Starting Monday_af_date").valueAsString;
if(cMyDate != ""){
var oMyDate = util.scand("mm/dd/yyyyy", cMyDate);
if (oMyDate!=null) event.value = getDayOfYear(oMyDate)+0; //increase by necessary interval.
}

 

Calendar.PNG

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

What format does you use at field "Starting Monday_af_date" ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

The format of the source date is: mm/dd/yyyy

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

The function getDayOfYear is correct.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

I am not having an issue with that function.  My issue is adjusting the source date within the code to account for the year change.  The calendar I created in the document autofills based on one date that is not displayed in the picture below (Monday is +0, Tuesday +1, Wednesday, +2, etc.).  Getting the dates to calculate correctly and in the desired format wasn't difficult, and the code I was given works great for getting the day of the year.  The only thing it doesn't take into consideration is that it converts the date to day of the year (as in my picture 29 Dec becomes 363) and then adds the applicable interval to determine appropriate day of the year based on location in the calendar (again in the picture 29 Dec is 363 and then adds 4 so that Jan 2 becomes 367).  What I'm trying to figure out is how do I adjust the date within the code so that it converts the final date to day of the year (29 Dec plus 4 days equals 2 Jan to become 2).  I really hope that makes sense, I'm finding it difficult to explain in this format.Calendar.PNG

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

Add a condition that if the result is more than 365, reduce 365 from it...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

I can't believe I didn't think of that, thank you.  You wouldn't happen to have a sample of the code for that you could post for me to base mine on?  My abilities in Java are virtually 0.  Thanks again.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

I just realised this solution would not account for leap year, unless there's a way to include that as a condition.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

You can use the isLeapYear function for that. If the second date is in a leap year (and after March 1st) then add 1 to the final result.

But I don't understand what you're trying to achieve... Do you want to calculate the number of days between two dates? Because they can be more than one year apart, you know... You need the full dates to be able to calculate that.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

I'm not calculating between two dates, just creating a calendar based on one date.  The issue I'm having is the transition between years and the DayOfYear doesn't return to 1 if the schedule starts in the previous year.  There's only a max of 32 days between the start and end.Calendar.PNG 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

You need to change your code to use the actual date of each item in your calendar instead of an offset from a specific starting date.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

How do I do that?  When I attempt to change it to one of the other date boxes I get no value.  I have updated the correct field name and date format in the code.  Below is the example codes that produces no result.

Date Box Code:

event.value = ""; // clear field
var date=new Date(this.getField("Starting Monday_af_date").value);
date.setDate(date.getDate()+1); //increment by -1 as needed: -2,-3, etc.
if(date !=""){
	var oMyDate = util.scand("mm/dd/yyyy", date);
	if (oMyDate!=null) event.value=util.printd("yyyymmdd", date);//change date format as required.
}

DayOfYear Box:

// define getDayOfYear function
function getDayOfYear(d) {
	var j1= new Date(d);
	j1.setMonth(0, 0, 0, 0, 0, 0);
	return Math.round((d-j1)/(1000 * 60 * 60 * 24));
}

event.value = ""; // clear field
var cMyDate = this.getField("Date 2").valueAsString;
if(cMyDate != ""){
	var oMyDate = util.scand("yyyyymmdd", cMyDate);
	if (oMyDate!=null) event.value = getDayOfYear(oMyDate);
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

The function getDayOfYear creates 2 for the date 01/02/2020

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 29, 2019 Oct 29, 2019

Copy link to clipboard

Copied

LATEST

Thanks for all the help everyone!  I got it working flawlessly now, I seriously couldn't have done it without your help.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines