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

应当忽略时间而只取年月日去重新构建 Date 来计算时间差 #7

Open
kagawagao opened this issue Sep 7, 2017 · 1 comment

Comments

@kagawagao
Copy link

kagawagao commented Sep 7, 2017

var day = _solarDiff(solar, firstDay, "d") + 1;

var span = left.getTime() - right.getTime(); //相差毫秒

case "d": return Math.ceil(span / 1000 / 60 / 60 / 24);

例子:

var a = new Date(2017, 7, 22) // 农历七月初一
var b = new Date('2017-9-1 16:20:20') // 农历七月十一
var c = new Date('2017-9-1')

var span1 = b.getTime() - a.getTime() // 922820000
var diff1 = Math.ceil(span1/ 1000 / 60 / 60 /24) // 结果为11,算上后面再加上的一天,那么农历则为七月十二,错误

var span2 = c.getTime() - a.getTime() // 864000000
var diff2 = Math.ceil(span2/ 1000 / 60 / 60 /24) // 结果为10,算上后面再加上的一天,那么农历则为七月十一,正确

最后在 switch-case 也不需要 Math.ceil

@wolfg1969
Copy link

我用 moment.js 把时间设为 0 点 再传入计算农历

const lunar = chineseLunar.solarToLunar(moment().startOf('day').toDate(), 'MD');

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