diff --git a/src/diff.js b/src/diff.js index 2fc56c4..0237ceb 100644 --- a/src/diff.js +++ b/src/diff.js @@ -13,16 +13,32 @@ function daysToYears(days) { return days * 400 / 146097; } +function stripLocaleTime(ms) { + var date = new Date(ms); + date.setHours(0); + date.setMinutes(0); + date.setSeconds(0); + date.setMilliseconds(0); + return date.getTime(); +} + +function msToDay(ms) { + return ms / (1000 * 60 * 60 * 24); +} + export default function (from, to) { // Convert to ms timestamps. from = +from; to = +to; + var fromDay = stripLocaleTime(from), + toDay = stripLocaleTime(to); + var millisecond = round(to - from), second = round(millisecond / 1000), minute = round(second / 60), hour = round(minute / 60), - day = round(hour / 24), + day = round(msToDay(toDay - fromDay)), week = round(day / 7); var rawYears = daysToYears(day), diff --git a/tests/index.js b/tests/index.js index 8680f00..3c18aa9 100644 --- a/tests/index.js +++ b/tests/index.js @@ -157,6 +157,13 @@ describe('IntlRelativeFormat', function () { expect(rf.format(future(30 * 24 * 60 * 60 * 1000))).to.equal('in 30 days'); }); + it('should output yesterday if the date is at 11:59:59pm the day and now is midnight', function () { + var midnight = (new Date(2017, 9, 2)).getTime(); + var yesterday = midnight - 1; + var rf = new IntlRelativeFormat('en', {units: 'day'}); + expect(rf.format(yesterday, { now: midnight })).to.equal('yesterday'); + }); + it('should handle short unit formats', function () { var rf = new IntlRelativeFormat('en', {units: 'minute-short'});