date.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * Represent a date as a short/default/long/full 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('Today is {T, date}')({ T: Date.now() })
  12. * // 'Today is Feb 21, 2016'
  13. *
  14. * mf.compile('Tänään on {T, date}', 'fi')({ T: Date.now() })
  15. * // 'Tänään on 21. helmikuuta 2016'
  16. *
  17. * mf.compile('Unix time started on {T, date, full}')({ T: 0 })
  18. * // 'Unix time started on Thursday, January 1, 1970'
  19. *
  20. * var cf = mf.compile('{sys} became operational on {d0, date, short}');
  21. * cf({ sys: 'HAL 9000', d0: '12 January 1999' })
  22. * // 'HAL 9000 became operational on 1/12/1999'
  23. * ```
  24. */
  25. export function date(value, lc, size) {
  26. var o = {
  27. day: 'numeric',
  28. month: 'short',
  29. year: 'numeric'
  30. };
  31. /* eslint-disable no-fallthrough */
  32. switch (size) {
  33. case 'full':
  34. o.weekday = 'long';
  35. case 'long':
  36. o.month = 'long';
  37. break;
  38. case 'short':
  39. o.month = 'numeric';
  40. }
  41. return new Date(value).toLocaleDateString(lc, o);
  42. }