Convert unix timestamp to julian

后端 未结 3 1999
迷失自我
迷失自我 2020-12-30 02:19

How can I convert from a unix timestamp (say 1232559922) to a fractional julian date (2454853.03150).

I found a website ( http://aa.usno.navy.mil/data/docs/JulianDat

3条回答
  •  梦毁少年i
    2020-12-30 02:36

    Here is my JavaScript code to convert Unix timestamp to Julian. Originally is showing the current date and time, but with a little mod is answer to your question:

    function computeJulianDate(DD,MM,YY,HR,MN,SC) {
        with (Math) {
            HR = HR + (MN / 60) + (SC/3600);
            GGG = 1;
            if (YY <= 1585) GGG = 0;
            JD = -1 * floor(7 * (floor((MM + 9) / 12) + YY) / 4);
            S = 1;
            if ((MM - 9)<0) S=-1;
            A = abs(MM - 9);
            J1 = floor(YY + S * floor(A / 7));
            J1 = -1 * floor((floor(J1 / 100) + 1) * 3 / 4);
            JD = JD + floor(275 * MM / 9) + DD + (GGG * J1);
            JD = JD + 1721027 + 2 * GGG + 367 * YY - 0.5;
            JD = JD + (HR / 24);
        }
        return JD;
    }
    function getUTCDateTimeOrJD(now,jd=0) {
        var hours = now.getUTCHours();
        var minutes = now.getUTCMinutes();
        var seconds = now.getUTCSeconds()
        var month = now.getUTCMonth() + 1;
        var day = now.getUTCDate();
        var year = now.getUTCFullYear();
        if (jd==1)
            return computeJulianDate(month, day, year, hours, minutes, seconds);
        else
            return day+". "+month+". "+year+". "+hours+":"+minutes+":"+seconds;
    }
    
    var unixTime = 1473294606;
    
        getUTCDateTimeOrJD(new Date(unixTime*1000));
        getUTCDateTimeOrJD(new Date(unixTime*1000),1);
    

    Working JSFiddle example here

提交回复
热议问题