JavaScript如何将儒略日期转换为公历?含1830-02-24转换需求
Hey there! Let's break down your Julian to Gregorian date conversion questions in JavaScript—super practical stuff for historical date handling.
1. How to Convert Julian Calendar Dates to Gregorian in JavaScript
The most reliable way to handle this conversion is using Julian Day Numbers (JD) as an intermediate step. Here's why: JD is a continuous count of days since an ancient epoch, so we can first translate a Julian calendar date to its JD value, then convert that JD to a Gregorian calendar date.
Step-by-Step Implementation
First, we'll write helper functions to handle the JD conversion, then wrap them into a main function for easy use:
// Convert a Julian Calendar date to Julian Day Number (JD) function julianCalendarToJD(year, month, day) { // Adjust for months Jan/Feb being counted as months 13/14 of the previous year if (month <= 2) { year--; month += 12; } // No correction term needed for Julian Calendar (unlike Gregorian) const correction = 0; return Math.floor(365.25 * (year + 4716)) + Math.floor(30.6001 * (month + 1)) + day + correction - 1524.5; } // Convert Julian Day Number (JD) to Gregorian Calendar date function jdToGregorian(jd) { const z = Math.floor(jd + 0.5); const fractionalDay = jd + 0.5 - z; let a = z; if (z >= 2299161) { const alpha = Math.floor((z - 1867216.25) / 36524.25); a = z + 1 + alpha - Math.floor(alpha / 4); } const b = a + 1524; const c = Math.floor((b - 122.1) / 365.25); const d = Math.floor(365.25 * c); const e = Math.floor((b - d) / 30.6001); const day = b - d - Math.floor(30.6001 * e) + fractionalDay; const month = e < 14 ? e - 1 : e - 13; const year = month > 2 ? c - 4716 : c - 4715; return { year: Math.floor(year), month: Math.floor(month), day: Math.floor(day) }; } // Main function: Julian Calendar → Gregorian Calendar function julianToGregorian(year, month, day) { const jd = julianCalendarToJD(year, month, day); return jdToGregorian(jd); }
Quick Test Example
Let's verify with the famous 1582 transition date: Julian Calendar October 4th should convert to Gregorian October 15th:
console.log(julianToGregorian(1582, 10, 4)); // Output: { year: 1582, month: 10, day: 15 }
2. Converting the Specific Julian Date 1830-02-24 to Gregorian
For your exact date (Julian Calendar 1830-02-24), just plug the values into our julianToGregorian function. At this point in history, the Julian Calendar was 12 days behind the Gregorian, so we expect a March date as the result.
Code Call & Result
const julianDate = { year: 1830, month: 2, day: 24 }; const gregorianResult = julianToGregorian(julianDate.year, julianDate.month, julianDate.day); console.log(`Julian ${julianDate.year}-${julianDate.month}-${julianDate.day} → Gregorian ${gregorianResult.year}-${gregorianResult.month}-${gregorianResult.day}`); // Output: Julian 1830-2-24 → Gregorian 1830-3-8
That's spot-on—adding 12 days to Julian February 24th lands us on Gregorian March 8th.
内容的提问来源于stack exchange,提问作者Jarosław Goszowski

