JSFiddle - React, Tailwind, and code Playground
by dtrooper
TypeScript
import { getLunar } from 'chinese-lunar-calendar'
function chineseToGregorian(year, month, day, isLeapMonth = false) {
const result = getLunar(year, month, day, isLeapMonth)
return `${result.cYear}-${String(result.cMonth).padStart(2, "0")}-${String(result.cDay).padStart(2, "0")}`
}
// Example Usage:
const lunarYear = 2024
const lunarMonth = 1
const lunarDay = 1
const isLeap = false // Set to true if it's a leap month
const gregorianDate = chineseToGregorian(
lunarYear,
lunarMonth,
lunarDay,
isLeap,
)
console.log(
`Lunar ${lunarYear}-${lunarMonth}-${lunarDay} ${isLeap ? "(Leap)" : ""} -> Gregorian ${gregorianDate}`,
)