Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q808 How do I convert a date from YYMMDD to YYYYDDD?

You are here: irt.org | FAQ | JavaScript | Date | Q808 [ previous next ]

Try something like:

<SCRIPT LANGUAGE="JavaScript"><!--
function makeArray() {
    this[0] = makeArray.arguments.length;
    for (i = 0; i<makeArray.arguments.length; i++)
        this[i+1] = makeArray.arguments[i];
}

var accumulate    = new makeArray(0, 31, 59, 90,120,151,181,212,243,273,304,334);
var accumulateLY  = new makeArray(0, 31, 60, 91,121,152,182,213,244,274,305,335);

function LeapYear(year) {
    if ((year/4)   != Math.floor(year/4))   return false;
    if ((year/100) != Math.floor(year/100)) return true;
    if ((year/400) != Math.floor(year/400)) return false;
    return true;
}

function pad(number,length) {
    var str = '' + number;
    while (str.length < length)
        str = '0' + str;
    return str;
}

function getJulianDay(year,month,day) {
    if (LeapYear(year))
            return pad(day + accumulateLY[month],3);
    else
            return pad(day + accumulate[month],3);
}

function yy_to_ccyy(yy) {
    // convert year in yy format to ccyy format
    return (yy < 70) ? '20' + pad(yy,2) : '19' + yy;
}

var myDate = '000229'; // 29th February 2000

var year = yy_to_ccyy(myDate.substring(0,2) - 0);
var month = myDate.substring(2,4) - 0;
var day = myDate.substring(4,6) - 0;

var julianDate = '' + year + getJulianDay(year,month,day);

alert(julianDate);
//--></SCRIPT>

Feedback on 'Q808 How do I convert a date from YYMMDD to YYYYDDD?'

©2018 Martin Webb