index.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. var buildFormattingTokensRegExp = require('../../_lib/build_formatting_tokens_reg_exp/index.js')
  2. function buildFormatLocale () {
  3. // Note: in English, the names of days of the week and months are capitalized.
  4. // If you are making a new locale based on this one, check if the same is true for the language you're working on.
  5. // Generally, formatted dates should look like they are in the middle of a sentence,
  6. // e.g. in Spanish language the weekdays and months should be in the lowercase.
  7. var months3char = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  8. var monthsFull = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
  9. var weekdays2char = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']
  10. var weekdays3char = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
  11. var weekdaysFull = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
  12. var meridiemUppercase = ['AM', 'PM']
  13. var meridiemLowercase = ['am', 'pm']
  14. var meridiemFull = ['a.m.', 'p.m.']
  15. var formatters = {
  16. // Month: Jan, Feb, ..., Dec
  17. 'MMM': function (date) {
  18. return months3char[date.getMonth()]
  19. },
  20. // Month: January, February, ..., December
  21. 'MMMM': function (date) {
  22. return monthsFull[date.getMonth()]
  23. },
  24. // Day of week: Su, Mo, ..., Sa
  25. 'dd': function (date) {
  26. return weekdays2char[date.getDay()]
  27. },
  28. // Day of week: Sun, Mon, ..., Sat
  29. 'ddd': function (date) {
  30. return weekdays3char[date.getDay()]
  31. },
  32. // Day of week: Sunday, Monday, ..., Saturday
  33. 'dddd': function (date) {
  34. return weekdaysFull[date.getDay()]
  35. },
  36. // AM, PM
  37. 'A': function (date) {
  38. return (date.getHours() / 12) >= 1 ? meridiemUppercase[1] : meridiemUppercase[0]
  39. },
  40. // am, pm
  41. 'a': function (date) {
  42. return (date.getHours() / 12) >= 1 ? meridiemLowercase[1] : meridiemLowercase[0]
  43. },
  44. // a.m., p.m.
  45. 'aa': function (date) {
  46. return (date.getHours() / 12) >= 1 ? meridiemFull[1] : meridiemFull[0]
  47. }
  48. }
  49. // Generate ordinal version of formatters: M -> Mo, D -> Do, etc.
  50. var ordinalFormatters = ['M', 'D', 'DDD', 'd', 'Q', 'W']
  51. ordinalFormatters.forEach(function (formatterToken) {
  52. formatters[formatterToken + 'o'] = function (date, formatters) {
  53. return ordinal(formatters[formatterToken](date))
  54. }
  55. })
  56. return {
  57. formatters: formatters,
  58. formattingTokensRegExp: buildFormattingTokensRegExp(formatters)
  59. }
  60. }
  61. function ordinal (number) {
  62. var rem100 = number % 100
  63. if (rem100 > 20 || rem100 < 10) {
  64. switch (rem100 % 10) {
  65. case 1:
  66. return number + 'st'
  67. case 2:
  68. return number + 'nd'
  69. case 3:
  70. return number + 'rd'
  71. }
  72. }
  73. return number + 'th'
  74. }
  75. module.exports = buildFormatLocale