How to convert gregorian date to chinese lunisolar date, no need to explain in detail…
- User
How to convert gregorian date to chinese lunisolar date, no need to explain in detail about chinese calendar, just write an algorithm in javascript
Assistant
Here is an algorithm in JavaScript to convert a Gregorian date to a Chinese lunisolar date:
function gregorianToChinese(year, month, day) { // Constants for Chinese calendar const lunarMonthDays = [30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30]; // Determine lunar year let lunarYear = year; if (month < 2) { lunarYear--; } // Determine lunar month let lunarMonth; let totalDays = 0; for (let i = 1; i <= 12; i++) { totalDays += lunarMonthDays[i-1]; if (totalDays > (month-1)*31 + day) { lunarMonth = i; break; } } // Calculate lunar day let lunarDay = day - (totalDays - (month-1)*31); // Adjust for leap month if (lunarMonth === 12 && lunarDay > 29) { lunarMonth = 1; lunarYear++; } return { lunarYear, lunarMonth, lunarDay }; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.