a79b0172efe81d7ff9d72bda026a64fcdaab82f02c8ecf5b7544a702b1500dff02fd947091713a8dcd72bb7ecbe8a912c4ad402a22902ef657a0f7be11548d 2.3 KB

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