Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] create setTime function #3620

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/lib/moment/start-end-of.js
@@ -1,4 +1,5 @@
import { normalizeUnits } from '../units/aliases';
import { setTime } from '../units/hour';

export function startOf (units) {
units = normalizeUnits(units);
Expand All @@ -16,8 +17,8 @@ export function startOf (units) {
case 'isoWeek':
case 'day':
case 'date':
this.hours(0);
/* falls through */
setTime(this, 0, 0, 0, 0);
break;
case 'hour':
this.minutes(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the hour case should be this.setTime(this, this.hour(), 0, 0, 0), etc

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True! I also need to benchmark the functions setMinutes(minutes, seconds, milliseconds) and setSeconds(seconds, milliseconds) to see if they are even more efficient.

/* falls through */
Expand Down
8 changes: 8 additions & 0 deletions src/lib/units/hour.js
Expand Up @@ -8,6 +8,7 @@ import { HOUR, MINUTE, SECOND } from './constants';
import toInt from '../utils/to-int';
import zeroFill from '../utils/zero-fill';
import getParsingFlags from '../create/parsing-flags';
import { hooks } from '../utils/hooks';

// FORMATTING

Expand Down Expand Up @@ -136,3 +137,10 @@ export function localeMeridiem (hours, minutes, isLower) {
// a new timezone) makes sense. Adding/subtracting hours does not follow
// this rule.
export var getSetHour = makeGetSet('Hours', true);

export function setTime (mom, hours, minutes, seconds, milliseconds) {
if (mom.isValid()) {
mom._d['set' + (mom._isUTC ? 'UTC' : '') + 'Hours'](hours, minutes, seconds, milliseconds);
hooks.updateOffset(mom, true);
}
}