Skip to main content
Participating Frequently
May 8, 2025
Answered

Automatic Julian Date/Expiration Date

  • May 8, 2025
  • 1 reply
  • 681 views

I'm seeking assistance creating a custom label which includes autmatically filling in that days Julian Date (L5106 for example) and then a separate field which contains an expeiration date 420 days from that day. I've used the below script in previous labels for the expiration date, but have never calculated a Julian date. Any assitance would be much appreciated! 

 

Validation Script:

var expFld = this.getField("Expiration");

if (event.value != "") { // check if populated else script will fail.

    var d = util.scand("mm/dd/yy", event.value); // gets selected date from "Today" field

    var DateNextYear = new Date(d.getTime());

    DateNextYear.setDate(d.getDate() + 420);

    expFld.value = util.printd("mm/dd/yy", DateNextYear); //assign future date to "Expiration" field

} else expFld.value = "";

 

Javascript Code:

var f = this.getField("Today");f.value = util.printd("mm/dd/yy", new Date());

Correct answer Nesa Nurani

Assuming your Julian date field is named "Julian Date" (please update the script with your actual field name if different), you can place the following code in the Validate script of the date field. This script will calculate both the expiration date and the Julian date:

var expFld = this.getField("Expiration");
var julianFld = this.getField("Julian Date");

if (event.value != "") {
 var d = util.scand("mm/dd/yy", event.value);
 var DateNextYear = new Date(d.getTime());
 DateNextYear.setDate(DateNextYear.getDate() + 420);
 expFld.value = util.printd("mm/dd/yy", DateNextYear);

 var startOfYear = new Date(d.getFullYear(), 0, 1);
 var tempDate = new Date(startOfYear);
 var dayOfYear = 0;
  while (tempDate <= d) {
   dayOfYear++;
    tempDate.setDate(tempDate.getDate() + 1);}

 var year = d.getFullYear();
 var julian = "L" + String(year).slice(-1) + ("00" + dayOfYear).slice(-3);
  julianFld.value = julian;} 
else{
 expFld.value = "";
 julianFld.value = "";}

1 reply

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
May 8, 2025

Assuming your Julian date field is named "Julian Date" (please update the script with your actual field name if different), you can place the following code in the Validate script of the date field. This script will calculate both the expiration date and the Julian date:

var expFld = this.getField("Expiration");
var julianFld = this.getField("Julian Date");

if (event.value != "") {
 var d = util.scand("mm/dd/yy", event.value);
 var DateNextYear = new Date(d.getTime());
 DateNextYear.setDate(DateNextYear.getDate() + 420);
 expFld.value = util.printd("mm/dd/yy", DateNextYear);

 var startOfYear = new Date(d.getFullYear(), 0, 1);
 var tempDate = new Date(startOfYear);
 var dayOfYear = 0;
  while (tempDate <= d) {
   dayOfYear++;
    tempDate.setDate(tempDate.getDate() + 1);}

 var year = d.getFullYear();
 var julian = "L" + String(year).slice(-1) + ("00" + dayOfYear).slice(-3);
  julianFld.value = julian;} 
else{
 expFld.value = "";
 julianFld.value = "";}
Participating Frequently
May 8, 2025

This worked great! Thanks so much for the help.  One issue: I was double checking the Julian Date and it appears that the Julian date isn't changing from 3/9-3/10 (it gets stuck at L5068) meaning it's off by a day thereafter.  Do you know what could be causing this?

try67
Community Expert
Community Expert
May 8, 2025

Could be due to the change to Daylight Saving Time (depending on your locale). That is the problem with using the method described above to changing dates, as there's an extra hour in that day (and one less in the opposite date, where it falls back to Winter Time), so it doesn't "switch over" correctly. It's better to use the setDate method of the Date object, instead.