# Date to Ordinal Days #### June 15, 2009

I recently had to create a reasonably meaningful batch number for some GS1 compliant SSCC labels.

I have an Internet Explorer Interface that submits the request to our ERP system.

Due to legacy issues the batch number was restricted to 6 numeric digits. Some colleagues and I ended up coming up with a format of:

YOOOBB

Where Y is the right most digit of the current year (2009 = 9)

Where OOO is the zero filled ordinal day of the year (January 1 = 001 , 15 June 2009 = 166)

And BB is a user interface selected 2 digit batch no. (01, 02, 0x – 99)

To get the Ordinal Day of the year was an excercise in Googling. Here is the algorithm I found embedded in someones html page… Sadly I can’t attribute because I can’t seem to Google back to it. So if it’s yours let me know and I will.

Javascript Ordinal Date

```

function isLeapYear(y) {
// check to to see if the year is divisible by 4 and therefore a Leap Year
if (y%4==0) {
if (y%100==0) return (y%400==0)?true:false;
return true;
}
return false;
}

function Right(str, n){
// This is a javascript right function
if (n <= 0)
return "";
else if (n > String(str).length)
return str;
else {
var iLen = String(str).length;
return String(str).substring(iLen, iLen - n);
}
}

function getBatchNo() {
//this does the work of returning the ordinal day or year
var days = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var t = new Date();
var yr = t.getFullYear();
var mon = t.getMonth();
var day = t.getDate();

// calc julian date
// if leap year add + 1 day to february days value
if (isLeapYear(yr)) days+=1;
var jDate = 0;
for (var i=0; i<mon; i++) jDate+=days[i];
jDate+=day;
if (jDate<100) jDate="0"+jDate;
if (jDate<10) jDate="0"+jDate;

digit1 = Right(yr, 1);

return digit1 + jDate;

}

var batch = "01";

```

VBScript Ordinal Date

```myDate = Date
days = Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
' now calc date
y = year(myDate)
' array is zero based so make
' the month the same 0 = jan, 1 = feb etc
m = month(myDate) - 1
d = day(myDate)

' find out if it's a Leap Year
if ( 0 = y mod 4 ) then
' add one day to february if it's a leap yr
days(1) = days(1) + 1
end if

for i = 0 to m - 1
' if june you need to add the first 5 months totals
myDays = myDays + days(i)
next

'then add the final months day value
myDays = myDays + d

msgbox "Date : " & date & vbcrlf & "Ordinal Days : " & myDays
```