return this.clone().locale('en').format('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ');
}
-export function toISOString() {
+export function toISOString(keepOffset) {
if (!this.isValid()) {
return null;
}
- var m = this.clone().utc();
+ var utc = keepOffset !== true;
+ var m = utc ? this.clone().utc() : this;
if (m.year() < 0 || m.year() > 9999) {
- return formatMoment(m, 'YYYYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
+ return formatMoment(m, utc ? 'YYYYYY-MM-DD[T]HH:mm:ss.SSS[Z]' : 'YYYYYY-MM-DD[T]HH:mm:ss.SSSZ');
}
if (isFunction(Date.prototype.toISOString)) {
// native implementation is ~50x faster, use it when we can
- return this.toDate().toISOString();
+ if (utc) {
+ return this.toDate().toISOString();
+ } else {
+ return new Date(this._d.valueOf()).toISOString().replace('Z', formatMoment(m, 'Z'));
+ }
}
- return formatMoment(m, 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
+ return formatMoment(m, utc ? 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]' : 'YYYY-MM-DD[T]HH:mm:ss.SSSZ');
}
/**
assert.equal(date.toISOString(), null, 'An invalid date to iso string is null');
});
+test('toISOString without UTC conversion', function (assert) {
+ var date = moment.utc('2016-12-31T19:53:45.678').utcOffset('+05:30');
+
+ assert.equal(date.toISOString(true), '2017-01-01T01:23:45.678+05:30', 'should output ISO8601 on moment.fn.toISOString');
+
+ // big years
+ date = moment.utc('+020122-12-31T19:53:45.678').utcOffset('+05:30');
+ assert.equal(date.toISOString(true), '+020123-01-01T01:23:45.678+05:30', 'ISO8601 format on big positive year');
+ // negative years
+ date = moment.utc('-000002-12-31T19:53:45.678').utcOffset('+05:30');
+ assert.equal(date.toISOString(true), '-000001-01-01T01:23:45.678+05:30', 'ISO8601 format on negative year');
+ // big negative years
+ date = moment.utc('-020124-12-31T19:53:45.678').utcOffset('+05:30');
+ assert.equal(date.toISOString(true), '-020123-01-01T01:23:45.678+05:30', 'ISO8601 format on big negative year');
+
+ //invalid dates
+ date = moment.utc('2017-12-32').utcOffset('+05:30');
+ assert.equal(date.toISOString(true), null, 'An invalid date to iso string is null');
+});
+
// See https://nodejs.org/dist/latest/docs/api/util.html#util_custom_inspect_function_on_objects
test('inspect', function (assert) {
function roundtrip(m) {