index.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. var buildFormattingTokensRegExp = require('../../_lib/build_formatting_tokens_reg_exp/index.js')
  2. function buildFormatLocale () {
  3. var months3char = ['jan', 'feb', 'mar', 'apr', 'maj', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dec']
  4. var monthsFull = ['januari', 'februari', 'mars', 'april', 'maj', 'juni', 'juli', 'augusti', 'september', 'oktober', 'november', 'december']
  5. var weekdays2char = ['sö', 'må', 'ti', 'on', 'to', 'fr', 'lö']
  6. var weekdays3char = ['sön', 'mån', 'tis', 'ons', 'tor', 'fre', 'lör']
  7. var weekdaysFull = ['söndag', 'måndag', 'tisdag', 'onsdag', 'torsdag', 'fredag', 'lördag']
  8. var meridiemFull = ['f.m.', 'e.m.']
  9. var formatters = {
  10. // Month: Jan, Feb, ..., Dec
  11. 'MMM': function (date) {
  12. return months3char[date.getMonth()]
  13. },
  14. // Month: January, February, ..., December
  15. 'MMMM': function (date) {
  16. return monthsFull[date.getMonth()]
  17. },
  18. // Day of week: Su, Mo, ..., Sa
  19. 'dd': function (date) {
  20. return weekdays2char[date.getDay()]
  21. },
  22. // Day of week: Sun, Mon, ..., Sat
  23. 'ddd': function (date) {
  24. return weekdays3char[date.getDay()]
  25. },
  26. // Day of week: Sunday, Monday, ..., Saturday
  27. 'dddd': function (date) {
  28. return weekdaysFull[date.getDay()]
  29. },
  30. // a.m., p.m.
  31. 'aa': function (date) {
  32. return (date.getHours() / 12) >= 1 ? meridiemFull[1] : meridiemFull[0]
  33. }
  34. }
  35. formatters.A = formatters.aa
  36. formatters.a = formatters.aa
  37. // Generate ordinal version of formatters: M -> Mo, D -> Do, etc.
  38. var ordinalFormatters = ['M', 'D', 'DDD', 'd', 'Q', 'W']
  39. ordinalFormatters.forEach(function (formatterToken) {
  40. formatters[formatterToken + 'o'] = function (date, formatters) {
  41. return ordinal(formatters[formatterToken](date))
  42. }
  43. })
  44. return {
  45. formatters: formatters,
  46. formattingTokensRegExp: buildFormattingTokensRegExp(formatters)
  47. }
  48. }
  49. function ordinal (number) {
  50. var rem100 = number % 100
  51. if (rem100 > 20 || rem100 < 10) {
  52. switch (rem100 % 10) {
  53. case 1:
  54. case 2:
  55. return number + ':a'
  56. }
  57. }
  58. return number + ':e'
  59. }
  60. module.exports = buildFormatLocale