index.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. var buildFormattingTokensRegExp = require('../../_lib/build_formatting_tokens_reg_exp/index.js')
  2. function buildFormatLocale () {
  3. var months3char = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
  4. var monthsFull = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
  5. var weekdays2char = ['日', '月', '火', '水', '木', '金', '土']
  6. var weekdays3char = ['日曜', '月曜', '火曜', '水曜', '木曜', '金曜', '土曜']
  7. var weekdaysFull = ['日曜日', '月曜日', '火曜日', '水曜日', '木曜日', '金曜日', '土曜日']
  8. var meridiemUppercase = ['午前', '午後']
  9. var meridiemLowercase = ['午前', '午後']
  10. var meridiemFull = ['午前', '午後']
  11. var formatters = {
  12. // Month: Jan, Feb, ..., Dec
  13. 'MMM': function (date) {
  14. return months3char[date.getMonth()]
  15. },
  16. // Month: January, February, ..., December
  17. 'MMMM': function (date) {
  18. return monthsFull[date.getMonth()]
  19. },
  20. // Day of week: Su, Mo, ..., Sa
  21. 'dd': function (date) {
  22. return weekdays2char[date.getDay()]
  23. },
  24. // Day of week: Sun, Mon, ..., Sat
  25. 'ddd': function (date) {
  26. return weekdays3char[date.getDay()]
  27. },
  28. // Day of week: Sunday, Monday, ..., Saturday
  29. 'dddd': function (date) {
  30. return weekdaysFull[date.getDay()]
  31. },
  32. // AM, PM
  33. 'A': function (date) {
  34. return (date.getHours() / 12) >= 1 ? meridiemUppercase[1] : meridiemUppercase[0]
  35. },
  36. // am, pm
  37. 'a': function (date) {
  38. return (date.getHours() / 12) >= 1 ? meridiemLowercase[1] : meridiemLowercase[0]
  39. },
  40. // a.m., p.m.
  41. 'aa': function (date) {
  42. return (date.getHours() / 12) >= 1 ? meridiemFull[1] : meridiemFull[0]
  43. }
  44. }
  45. // Generate ordinal version of formatters: M -> Mo, D -> Do, etc.
  46. var ordinalFormatters = ['M', 'D', 'DDD', 'd', 'Q', 'W']
  47. ordinalFormatters.forEach(function (formatterToken) {
  48. formatters[formatterToken + 'o'] = function (date, formatters) {
  49. return ordinal(formatters[formatterToken](date))
  50. }
  51. })
  52. return {
  53. formatters: formatters,
  54. formattingTokensRegExp: buildFormattingTokensRegExp(formatters)
  55. }
  56. }
  57. function ordinal (number) {
  58. return number + '日'
  59. }
  60. module.exports = buildFormatLocale