return this.clone().locale('en').format('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ');
}
-export function toISOString () {
+export function toISOString() {
+ if (!this.isValid()) {
+ return null;
+ }
var m = this.clone().utc();
- if (0 < m.year() && m.year() <= 9999) {
- if (isFunction(Date.prototype.toISOString)) {
- // native implementation is ~50x faster, use it when we can
- return this.toDate().toISOString();
- } else {
- return formatMoment(m, 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
- }
- } else {
+ if (m.year() < 0 || m.year() > 9999) {
return formatMoment(m, 'YYYYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
}
+ if (isFunction(Date.prototype.toISOString)) {
+ // native implementation is ~50x faster, use it when we can
+ return this.toDate().toISOString();
+ }
+ return formatMoment(m, 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
}
/**
// big negative years
date = moment.utc('-020123-10-09T20:30:40.678');
assert.equal(date.toISOString(), '-020123-10-09T20:30:40.678Z', 'ISO8601 format on big negative year');
+
+ //invalid dates
+ date = moment.utc('2017-12-32');
+ assert.equal(date.toISOString(), 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