lunar icon indicating copy to clipboard operation
lunar copied to clipboard

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

Open kagawagao opened this issue 7 years ago • 1 comments

https://github.com/wvv8oo/lunar/blob/34940a8a0c3db14d35e973ab53b9d86a89542fce/lib/chinese-lunar.js#L540

https://github.com/wvv8oo/lunar/blob/34940a8a0c3db14d35e973ab53b9d86a89542fce/lib/chinese-lunar.js#L244

https://github.com/wvv8oo/lunar/blob/34940a8a0c3db14d35e973ab53b9d86a89542fce/lib/chinese-lunar.js#L248

例子:

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

kagawagao avatar Sep 07 '17 09:09 kagawagao

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

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

wolfg1969 avatar Oct 02 '17 08:10 wolfg1969