Skip to content
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

Open
rainTai opened this issue Apr 8, 2023 · 5 comments
Open

建议:增加距离当前日期前后节气的日期数 #11

rainTai opened this issue Apr 8, 2023 · 5 comments

Comments

@rainTai
Copy link

rainTai commented Apr 8, 2023

比如23年4月8日当天属于清明节,春分换清明时间为4日,相对前个节气的日期距离为4。19日清明换谷雨,则19-8=11天,相对下个节气的日期距离为11

@rainTai
Copy link
Author

rainTai commented Apr 8, 2023

说错了4月8日属于清明节气

@rainTai
Copy link
Author

rainTai commented Apr 8, 2023

最好有三种类型获取,距离节,距离气,距离节+气

@waterbeside
Copy link
Owner

比如23年4月8日当天属于清明节,春分换清明时间为4日,相对前个节气的日期距离为4。19日清明换谷雨,则19-8=11天,相对下个节气的日期距离为11

您好,我想了解一下使用场境,再考虑有没必要加入该功能。

另外,你也可以先自行封装一个函数来解决你现在的需求。

以下这个函数是我对你当前需求的理解,可试试是否满足需求:

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}

@rainTai
Copy link
Author

rainTai commented Apr 9, 2023 via email

@waterbeside
Copy link
Owner

谢谢,我会尝试下函数是否满足需求,我之前的做法是取前后两个月的数据获取到对应的节令,然后用于八字的大运起运时间,非常感谢您的回复~

如果是起大运,上边的函数要改一下(不需要用到气)。

大运、流年的查法,本来是计划在@lunisolar/plugin-char8ex插件中的,不过我想先把寿星天文历插件写好,校正每个交节时间点之后再把功能补上去。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants