date.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.date = void 0;
  4. /**
  5. * Represent a date as a short/default/long/full string
  6. *
  7. * @param value Either a Unix epoch time in milliseconds, or a string value
  8. * representing a date. Parsed with `new Date(value)`
  9. *
  10. * @example
  11. * ```js
  12. * var mf = new MessageFormat(['en', 'fi']);
  13. *
  14. * mf.compile('Today is {T, date}')({ T: Date.now() })
  15. * // 'Today is Feb 21, 2016'
  16. *
  17. * mf.compile('Tänään on {T, date}', 'fi')({ T: Date.now() })
  18. * // 'Tänään on 21. helmikuuta 2016'
  19. *
  20. * mf.compile('Unix time started on {T, date, full}')({ T: 0 })
  21. * // 'Unix time started on Thursday, January 1, 1970'
  22. *
  23. * var cf = mf.compile('{sys} became operational on {d0, date, short}');
  24. * cf({ sys: 'HAL 9000', d0: '12 January 1999' })
  25. * // 'HAL 9000 became operational on 1/12/1999'
  26. * ```
  27. */
  28. function date(value, lc, size) {
  29. var o = {
  30. day: 'numeric',
  31. month: 'short',
  32. year: 'numeric'
  33. };
  34. /* eslint-disable no-fallthrough */
  35. switch (size) {
  36. case 'full':
  37. o.weekday = 'long';
  38. case 'long':
  39. o.month = 'long';
  40. break;
  41. case 'short':
  42. o.month = 'numeric';
  43. }
  44. return new Date(value).toLocaleDateString(lc, o);
  45. }
  46. exports.date = date;