Skip to content

Commit

Permalink
Doing a first aim at getting midnight after adding 1 day. If that tim…
Browse files Browse the repository at this point in the history
…e is not midnight, doing a second aim at midnight.
  • Loading branch information
keithwillcode committed Jan 13, 2017
1 parent b8a297c commit 1c2b5d2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/lib/moment/start-end-of.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,12 @@ export function endOf (units) {
units = 'day';
}

return this.startOf(units).add(1, (units === 'isoWeek' ? 'week' : units)).subtract(1, 'ms');
var firstAim = this.startOf(units).add(1, (units === 'isoWeek' ? 'week' : units));
var isMidnight = this.hours() === 0 && this.minutes() === 0;

if (isMidnight) {
return firstAim.subtract(1, 'ms');
}

return firstAim.startOf(units).subtract(1, 'ms');
}
17 changes: 16 additions & 1 deletion src/test/moment/start_end_of.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,22 @@ test('end of day', function (assert) {
assert.equal(m.hours(), 23, 'set the hours');
assert.equal(m.minutes(), 59, 'set the minutes');
assert.equal(m.seconds(), 59, 'set the seconds');
assert.equal(m.milliseconds(), 999, 'set the seconds');
assert.equal(m.milliseconds(), 999, 'set the milliseconds');
});

test('end of day with timezone on day DST switch occurs', function (assert) {
var m = moment(new Date(2016, 10, 16, 3, 4, 5, 6), 'America/Sao_Paulo').endOf('day'),
ms = moment(new Date(2016, 10, 16, 3, 4, 5, 6), 'America/Sao_Paulo').endOf('days'),
ma = moment(new Date(2016, 10, 16, 3, 4, 5, 6), 'America/Sao_Paulo').endOf('d');
assert.equal(+m, +ms, 'Plural or singular should work');
assert.equal(+m, +ma, 'Full or abbreviated should work');
assert.equal(m.year(), 2016, 'keep the year');
assert.equal(m.month(), 10, 'keep the month');
assert.equal(m.date(), 16, 'keep the day');
assert.equal(m.hours(), 23, 'set the hours');
assert.equal(m.minutes(), 59, 'set the minutes');
assert.equal(m.seconds(), 59, 'set the seconds');
assert.equal(m.milliseconds(), 999, 'set the milliseconds');
});

test('start of date', function (assert) {
Expand Down

0 comments on commit 1c2b5d2

Please sign in to comment.