Skip to content
This repository has been archived by the owner on Jul 10, 2019. It is now read-only.

Days diffs are calculated based on the beginning of the day #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
7 changes: 7 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'});

Expand Down