1import { formatMoment } from '../format/format';
2import { hooks } from '../utils/hooks';
3import isFunction from '../utils/is-function';
4
5hooks.defaultFormat = 'YYYY-MM-DDTHH:mm:ssZ';
6hooks.defaultFormatUtc = 'YYYY-MM-DDTHH:mm:ss[Z]';
7
8export function toString() {
9 return this.clone().locale('en').format('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ');
10}
11
12export function toISOString(keepOffset) {
13 if (!this.isValid()) {
14 return null;
15 }
16 var utc = keepOffset !== true,
17 m = utc ? this.clone().utc() : this;
18 if (m.year() < 0 || m.year() > 9999) {
19 return formatMoment(
20 m,
21 utc
22 ? 'YYYYYY-MM-DD[T]HH:mm:ss.SSS[Z]'
23 : 'YYYYYY-MM-DD[T]HH:mm:ss.SSSZ'
24 );
25 }
26 if (isFunction(Date.prototype.toISOString)) {
27 // native implementation is ~50x faster, use it when we can
28 if (utc) {
29 return this.toDate().toISOString();
30 } else {
31 return new Date(this.valueOf() + this.utcOffset() * 60 * 1000)
32 .toISOString()
33 .replace('Z', formatMoment(m, 'Z'));
34 }
35 }
36 return formatMoment(
37 m,
38 utc ? 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]' : 'YYYY-MM-DD[T]HH:mm:ss.SSSZ'
39 );
40}
41
42/**
43 * Return a human readable representation of a moment that can
44 * also be evaluated to get a new moment which is the same
45 *
46 * @link https://nodejs.org/dist/latest/docs/api/util.html#util_custom_inspect_function_on_objects
47 */
48export function inspect() {
49 if (!this.isValid()) {
50 return 'moment.invalid(/* ' + this._i + ' */)';
51 }
52 var func = 'moment',
53 zone = '',
54 prefix,
55 year,
56 datetime,
57 suffix;
58 if (!this.isLocal()) {
59 func = this.utcOffset() === 0 ? 'moment.utc' : 'moment.parseZone';
60 zone = 'Z';
61 }
62 prefix = '[' + func + '("]';
63 year = 0 <= this.year() && this.year() <= 9999 ? 'YYYY' : 'YYYYYY';
64 datetime = '-MM-DD[T]HH:mm:ss.SSS';
65 suffix = zone + '[")]';
66
67 return this.format(prefix + year + datetime + suffix);
68}
69
70export function format(inputString) {
71 if (!inputString) {
72 inputString = this.isUtc()
73 ? hooks.defaultFormatUtc
74 : hooks.defaultFormat;
75 }
76 var output = formatMoment(this, inputString);
77 return this.localeData().postformat(output);
78}