from()/to() wrong days calculation around a month
Author: pretenderCreated Aug 17, 2017Updated Sep 2, 2026
LabelsBugrelative time threshold issues
Description of the Issue and Steps to Reproduce:
Got the same result for the very different dates in to()/from():
const moment = require('moment')
var expiredAt = moment('2017-07-16T21:01:54.249Z')
var now = moment('2017-08-16T00:02:01.249Z')
var tomorrow = moment('2017-08-17T00:02:01.249Z')
now.to(expiredAt) // 'a month ago' (expected)
tomorrow.to(expiredAt) // 'a month ago' (expected)
moment.relativeTimeThreshold('d', 180)
now.to(expiredAt) // '30 days ago' (expected)
tomorrow.to(expiredAt) // '30 days ago' (unexpected)
Please, no. Maybe there are 30.00000001 and 30.99999999 values in math, let's see:
moment.relativeTimeRounding(v => { return v; })
now.to(expiredAt) // '30.125081018518518 days ago' (well, ok)
tomorrow.to(expiredAt) // '30.125081018518518 days ago'
God, please, no. They are the same!
expiredAt.format('x') // '1500238914249'
now.format('x') // '1502841721249'
console.log((1502841721249 - 1500238914249) / 1000 / 60 / 60 / 24) // 30.125081018518518
tomorrow.format('x') // '1502928121249'
console.log((1502928121249 - 1500238914249) / 1000 / 60 / 60 / 24) // 31.125081018518518
Environment:
Debian 8.7, Node.JS v4.4.7, Moment.JS v2.15.2, TZ='America/New_York' Debian 8.8, Node.JS v6.9.2, Moment.JS v2.18.1, TZ='Europe/Berlin'
Source: moment/moment