time.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.time = void 0;
  4. /**
  5. * Represent a time as a short/default/long 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('The time is now {T, time}')({ T: Date.now() })
  15. * // 'The time is now 11:26:35 PM'
  16. *
  17. * mf.compile('Kello on nyt {T, time}', 'fi')({ T: Date.now() })
  18. * // 'Kello on nyt 23.26.35'
  19. *
  20. * var cf = mf.compile('The Eagle landed at {T, time, full} on {T, date, full}');
  21. * cf({ T: '1969-07-20 20:17:40 UTC' })
  22. * // 'The Eagle landed at 10:17:40 PM GMT+2 on Sunday, July 20, 1969'
  23. * ```
  24. */
  25. function time(value, lc, size) {
  26. var o = {
  27. second: 'numeric',
  28. minute: 'numeric',
  29. hour: 'numeric'
  30. };
  31. /* eslint-disable no-fallthrough */
  32. switch (size) {
  33. case 'full':
  34. case 'long':
  35. o.timeZoneName = 'short';
  36. break;
  37. case 'short':
  38. delete o.second;
  39. }
  40. return new Date(value).toLocaleTimeString(lc, o);
  41. }
  42. exports.time = time;