function configFromInput(config) {
var input = config._i;
if (input === undefined) {
- config._d = new Date();
+ config._d = new Date(Moment.prototype.now());
} else if (isDate(input)) {
config._d = new Date(+input);
} else if (typeof input === 'string') {
import { weekOfYear, weeksInYear, dayOfYearFromWeeks } from '../units/week-calendar-utils';
import { YEAR, MONTH, DATE, HOUR, MINUTE, SECOND, MILLISECOND } from '../units/constants';
import { createLocal } from './local';
+import { Moment } from '../moment/constructor';
import defaults from '../utils/defaults';
import getParsingFlags from './parsing-flags';
function currentDateArray(config) {
- var now = new Date();
+ var nowValue = new Date(Moment.prototype.now());
if (config._useUTC) {
- return [now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()];
+ return [nowValue.getUTCFullYear(), nowValue.getUTCMonth(), nowValue.getUTCDate()];
}
- return [now.getFullYear(), now.getMonth(), now.getDate()];
+ return [nowValue.getFullYear(), nowValue.getMonth(), nowValue.getDate()];
}
// convert an array to a date.
import { createInvalid } from '../create/valid';
import { isMoment } from './constructor';
import { min, max } from './min-max';
+import { now } from './now';
import momentPrototype from './prototype';
function createUnix (input) {
}
export {
+ now,
min,
max,
isMoment,
--- /dev/null
+export var now = Date.now || function () {
+ return +(new Date());
+};
import { getSet } from './get-set';
import { locale, localeData, lang } from './locale';
import { prototypeMin, prototypeMax } from './min-max';
+import { now } from './now';
import { startOf, endOf } from './start-end-of';
import { valueOf, toDate, toArray, toObject, toJSON, unix } from './to-type';
import { isValid, parsingFlags, invalidAt } from './valid';
proto.localeData = localeData;
proto.max = prototypeMax;
proto.min = prototypeMin;
+proto.now = now;
proto.parsingFlags = parsingFlags;
proto.set = getSet;
proto.startOf = startOf;
import {
min,
max,
+ now,
isMoment,
momentPrototype as fn,
createUTC as utc,
moment.fn = fn;
moment.min = min;
moment.max = max;
+moment.now = now;
moment.utc = utc;
moment.unix = unix;
moment.months = months;
--- /dev/null
+import { module, test } from '../qunit';
+import moment from '../../moment';
+
+module('now');
+
+test('now', function (assert) {
+ var startOfTest = new Date().valueOf(),
+ momentNowTime = moment.now(),
+ afterMomentCreationTime = new Date().valueOf();
+
+ assert.ok(startOfTest <= momentNowTime, 'moment now() time should be now, not in the past');
+ assert.ok(momentNowTime <= afterMomentCreationTime, 'moment now() time should be now, not in the future');
+});
+
+test('now - custom value', function (assert) {
+ var CUSTOM_DATE = '2015-01-01T01:30:00.000Z';
+
+ var oldFn = moment.fn.now;
+
+ moment.fn.now = function () {
+ return new Date(CUSTOM_DATE).valueOf();
+ };
+
+ try {
+ assert.ok(moment().toISOString() === CUSTOM_DATE, 'moment() constructor should use the function defined by moment.now, but it did not');
+ assert.ok(moment([]).toISOString() === '2014-12-31T07:00:00.000Z', 'moment() constructor should fall back to the date defined by moment.now when an empty array is given, but it did not');
+ assert.ok(moment.utc([]).toISOString() === '2015-01-01T00:00:00.000Z', 'moment() constructor should fall back to the date defined by moment.now when an empty array is given, but it did not');
+ } finally {
+ moment.fn.now = oldFn;
+ }
+});