time.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /**
  2. * Represent a time as a short/default/long string
  3. *
  4. * @param value Either a Unix epoch time in milliseconds, or a string value
  5. * representing a date. Parsed with `new Date(value)`
  6. *
  7. * @example
  8. * ```js
  9. * var mf = new MessageFormat(['en', 'fi']);
  10. *
  11. * mf.compile('The time is now {T, time}')({ T: Date.now() })
  12. * // 'The time is now 11:26:35 PM'
  13. *
  14. * mf.compile('Kello on nyt {T, time}', 'fi')({ T: Date.now() })
  15. * // 'Kello on nyt 23.26.35'
  16. *
  17. * var cf = mf.compile('The Eagle landed at {T, time, full} on {T, date, full}');
  18. * cf({ T: '1969-07-20 20:17:40 UTC' })
  19. * // 'The Eagle landed at 10:17:40 PM GMT+2 on Sunday, July 20, 1969'
  20. * ```
  21. */
  22. export function time(value, lc, size) {
  23. var o = {
  24. second: 'numeric',
  25. minute: 'numeric',
  26. hour: 'numeric'
  27. };
  28. /* eslint-disable no-fallthrough */
  29. switch (size) {
  30. case 'full':
  31. case 'long':
  32. o.timeZoneName = 'short';
  33. break;
  34. case 'short':
  35. delete o.second;
  36. }
  37. return new Date(value).toLocaleTimeString(lc, o);
  38. }