-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
建议:增加距离当前日期前后节气的日期数 #11
Comments
说错了4月8日属于清明节气 |
最好有三种类型获取,距离节,距离气,距离节+气 |
您好,我想了解一下使用场境,再考虑有没必要加入该功能。 另外,你也可以先自行封装一个函数来解决你现在的需求。 以下这个函数是我对你当前需求的理解,可试试是否满足需求: type DayToSolarTermRes = {
solarTerm: lunisolar.SolarTerm // 当前所属的节气的SolarTerm对象
diff: number // 当前节气日期与当前日期的天数差
nextDiff: number // 下个节气日期与当天期的天数差
}
function dayToSolarTerm(lsr: lunisolar.Lunisolar): DayToSolarTermRes {
const year = lsr.year
const month = lsr.month
const day = lsr.day
const lsr0 = lunisolar(`${year}/${month}/${day}`) // 置为0点
// 最得最近节气数据
const solarTermData = lsr.recentSolarTerm(2)
const distanceA = lsr0.diff(solarTermData[1], 'day')
const res: DayToSolarTermRes = {
solarTerm: solarTermData[0],
diff: Math.abs(distanceA),
nextDiff: 0
}
// --- 计算nextDiff --
const SolarTerm = lunisolar.SolarTerm
const [currentJ, currentQ] = SolarTerm.getMonthTerms(year, month)
// 当天日期小于当月节
if (day < currentJ) res.nextDiff = currentJ - day
// 当天日期大于当月气
else if (day > currentQ) {
const nextMonthLsr = lsr.add(1, 'month')
const [nextJ, _] = SolarTerm.getMonthTerms(nextMonthLsr.year, nextMonthLsr.month)
res.nextDiff = lsr0.diff(
lunisolar(`${nextMonthLsr.year}/${nextMonthLsr.month}/${nextJ}`),
'day'
)
} else {
res.nextDiff = currentQ - day
}
return res
}
dayToSolarTerm(lunisolar('2023/04/09')) // {solarTerm: SolarTerm(清明), diff: 4, nextDiff: 11}
dayToSolarTerm(lunisolar('2023/04/01')) // {solarTerm: SolarTerm(春分), diff: 11, nextDiff: 4}
dayToSolarTerm(lunisolar('2023/04/30')) // {solarTerm: SolarTerm(穀雨), diff: 9, nextDiff: 6} |
谢谢,我会尝试下函数是否满足需求,我之前的做法是取前后两个月的数据获取到对应的节令,然后用于八字的大运起运时间,非常感谢您的回复~
…---- 回复的原邮件 ----
| 发件人 | Natron ***@***.***> |
| 日期 | 2023年04月09日 11:43 |
| 收件人 | ***@***.***> |
| 抄送至 | ***@***.***>***@***.***> |
| 主题 | Re: [waterbeside/lunisolar] 建议:增加距离当前日期前后节气的日期数 (Issue #11) |
比如23年4月8日当天属于清明节,春分换清明时间为4日,相对前个节气的日期距离为4。19日清明换谷雨,则19-8=11天,相对下个节气的日期距离为11
您好,我想了解一下使用场境,再考虑有没必要加入该功能。
另外,你也可以先自行封装一个函数来解决你现在的需求。
以下这个函数是我对你当前需求的理解,可试试是否满足需求:
typeDayToSolarTermRes={solarTerm: lunisolar.SolarTerm// 当前所属的节气的SolarTerm对象diff: number// 当前节气日期与当前日期的天数差nextDiff: number// 下个节气日期与当天期的天数差}functiondayToSolarTerm(lsr: lunisolar.Lunisolar): DayToSolarTermRes{constyear=lsr.yearconstmonth=lsr.monthconstday=lsr.dayconstlsr0=lunisolar(`${year}/${month}/${day}`)// 置为0点// 最得最近节气数据constsolarTermData=lsr.recentSolarTerm(2)constdistanceA=lsr0.diff(solarTermData[1],'day')constres: DayToSolarTermRes={solarTerm: solarTermData[0],diff: Math.abs(distanceA),nextDiff: 0}// --- 计算nextDiff --constSolarTerm=lunisolar.SolarTermconst[currentJ,currentQ]=SolarTerm.getMonthTerms(year,month)// 当天日期小于当月节if(day<currentJ)res.nextDiff=currentJ-day// 当天日期大于当月气elseif(day>currentQ){constnextMonthLsr=lsr.add(1,'month')const[nextJ,_]=SolarTerm.getMonthTerms(nextMonthLsr.year,nextMonthLsr.month)res.nextDiff=lsr0.diff(lunisolar(`${nextMonthLsr.year}/${nextMonthLsr.month}/${nextJ}`),'day')}else{res.nextDiff=currentQ-day}returnres}dayToSolarTerm(lunisolar('2023/04/09'))// {solarTerm: SolarTerm(清明), diff: 4, nextDiff: 11}dayToSolarTerm(lunisolar('2023/04/01'))// {solarTerm: SolarTerm(春分), diff: 11, nextDiff: 4}dayToSolarTerm(lunisolar('2023/04/30'))// {solarTerm: SolarTerm(穀雨), diff: 9, nextDiff: 6}
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
如果是起大运,上边的函数要改一下(不需要用到气)。 大运、流年的查法,本来是计划在@lunisolar/plugin-char8ex插件中的,不过我想先把寿星天文历插件写好,校正每个交节时间点之后再把功能补上去。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
比如23年4月8日当天属于清明节,春分换清明时间为4日,相对前个节气的日期距离为4。19日清明换谷雨,则19-8=11天,相对下个节气的日期距离为11
The text was updated successfully, but these errors were encountered: