strftime.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var pad = require('../number/pad');
  2. var lpad = require('../string/lpad');
  3. var i18n = require('./i18n_');
  4. var dayOfTheYear = require('./dayOfTheYear');
  5. var timezoneOffset = require('./timezoneOffset');
  6. var timezoneAbbr = require('./timezoneAbbr');
  7. var weekOfTheYear = require('./weekOfTheYear');
  8. var _combinations = {
  9. 'D': '%m/%d/%y',
  10. 'F': '%Y-%m-%d',
  11. 'r': '%I:%M:%S %p',
  12. 'R': '%H:%M',
  13. 'T': '%H:%M:%S',
  14. 'x': 'locale',
  15. 'X': 'locale',
  16. 'c': 'locale'
  17. };
  18. /**
  19. * format date based on strftime format
  20. */
  21. function strftime(date, format, localeData){
  22. localeData = localeData || i18n;
  23. var reToken = /%([a-z%])/gi;
  24. function makeIterator(fn) {
  25. return function(match, token){
  26. return fn(date, token, localeData);
  27. };
  28. }
  29. return format
  30. .replace(reToken, makeIterator(expandCombinations))
  31. .replace(reToken, makeIterator(convertToken));
  32. }
  33. function expandCombinations(date, token, l10n){
  34. if (token in _combinations) {
  35. var expanded = _combinations[token];
  36. return expanded === 'locale'? l10n[token] : expanded;
  37. } else {
  38. return '%'+ token;
  39. }
  40. }
  41. function convertToken(date, token, l10n){
  42. switch (token){
  43. case 'a':
  44. return l10n.days_abbr[date.getDay()];
  45. case 'A':
  46. return l10n.days[date.getDay()];
  47. case 'h':
  48. case 'b':
  49. return l10n.months_abbr[date.getMonth()];
  50. case 'B':
  51. return l10n.months[date.getMonth()];
  52. case 'C':
  53. return pad(Math.floor(date.getFullYear() / 100), 2);
  54. case 'd':
  55. return pad(date.getDate(), 2);
  56. case 'e':
  57. return pad(date.getDate(), 2, ' ');
  58. case 'H':
  59. return pad(date.getHours(), 2);
  60. case 'I':
  61. return pad(date.getHours() % 12, 2);
  62. case 'j':
  63. return pad(dayOfTheYear(date), 3);
  64. case 'l':
  65. return lpad(date.getHours() % 12, 2);
  66. case 'L':
  67. return pad(date.getMilliseconds(), 3);
  68. case 'm':
  69. return pad(date.getMonth() + 1, 2);
  70. case 'M':
  71. return pad(date.getMinutes(), 2);
  72. case 'n':
  73. return '\n';
  74. case 'p':
  75. return date.getHours() >= 12? l10n.pm : l10n.am;
  76. case 'P':
  77. return convertToken(date, 'p', l10n).toLowerCase();
  78. case 's':
  79. return date.getTime() / 1000;
  80. case 'S':
  81. return pad(date.getSeconds(), 2);
  82. case 't':
  83. return '\t';
  84. case 'u':
  85. var day = date.getDay();
  86. return day === 0? 7 : day;
  87. case 'U':
  88. return pad(weekOfTheYear(date), 2);
  89. case 'w':
  90. return date.getDay();
  91. case 'W':
  92. return pad(weekOfTheYear(date, 1), 2);
  93. case 'y':
  94. return pad(date.getFullYear() % 100, 2);
  95. case 'Y':
  96. return pad(date.getFullYear(), 4);
  97. case 'z':
  98. return timezoneOffset(date);
  99. case 'Z':
  100. return timezoneAbbr(date);
  101. case '%':
  102. return '%';
  103. default:
  104. // keep unrecognized tokens
  105. return '%'+ token;
  106. }
  107. }
  108. module.exports = strftime;