toDateString.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. var setupDefaults = require('./setupDefaults')
  2. var helperStringUpperCase = require('./helperStringUpperCase')
  3. var helperGetDateFullYear = require('./helperGetDateFullYear')
  4. var helperGetDateMonth = require('./helperGetDateMonth')
  5. var toStringDate = require('./toStringDate')
  6. var getYearWeek = require('./getYearWeek')
  7. var getYearDay = require('./getYearDay')
  8. var assign = require('./assign')
  9. var isValidDate = require('./isValidDate')
  10. var isFunction = require('./isFunction')
  11. var padStart = require('./padStart')
  12. function handleCustomTemplate (date, formats, match, value) {
  13. var format = formats[match]
  14. if (format) {
  15. if (isFunction(format)) {
  16. return format(value, match, date)
  17. } else {
  18. return format[value]
  19. }
  20. }
  21. return value
  22. }
  23. var dateFormatRE = /\[([^\]]+)]|y{2,4}|M{1,2}|d{1,2}|H{1,2}|h{1,2}|m{1,2}|s{1,2}|S{1,3}|Z{1,2}|W{1,2}|D{1,3}|[aAeEq]/g
  24. /**
  25. * 日期格式化为字符串,转义符号 []
  26. *
  27. * @param {Date} date 日期或数字
  28. * @param {String} format 输出日期格式(年份(yy|yyyy)、月份(M|MM自动补0)、天(d|dd自动补0)、12小时制(h|hh自动补0)、24小时制(H|HH自动补0)、分钟(m|mm自动补0)、秒(s|ss自动补0)、毫秒(S|SSS自动补0)、D当年的第几天、a/A上午下午、e/E星期几、w当年的第几周、W当月的第几周、q当年第几个季度、Z时区)
  29. * @param {Object} options {formats: {q: ['日', '一', '二', '三', '四', '五', '六'], E: function (value, match, date) {return '三'}, }} 自定义格式化模板
  30. * @return {String}
  31. */
  32. function toDateString (date, format, options) {
  33. if (date) {
  34. date = toStringDate(date)
  35. if (isValidDate(date)) {
  36. var result = format || setupDefaults.parseDateFormat || setupDefaults.formatString
  37. var hours = date.getHours()
  38. var apm = hours < 12 ? 'am' : 'pm'
  39. var formats = assign({}, setupDefaults.parseDateRules || setupDefaults.formatStringMatchs, options ? options.formats : null)
  40. var fy = function (match, length) {
  41. return ('' + helperGetDateFullYear(date)).substr(4 - length)
  42. }
  43. var fM = function (match, length) {
  44. return padStart(helperGetDateMonth(date) + 1, length, '0')
  45. }
  46. var fd = function (match, length) {
  47. return padStart(date.getDate(), length, '0')
  48. }
  49. var fH = function (match, length) {
  50. return padStart(hours, length, '0')
  51. }
  52. var fh = function (match, length) {
  53. return padStart(hours <= 12 ? hours : hours - 12, length, '0')
  54. }
  55. var fm = function (match, length) {
  56. return padStart(date.getMinutes(), length, '0')
  57. }
  58. var fs = function (match, length) {
  59. return padStart(date.getSeconds(), length, '0')
  60. }
  61. var fS = function (match, length) {
  62. return padStart(date.getMilliseconds(), length, '0')
  63. }
  64. var fZ = function (match, length) {
  65. var zoneHours = date.getTimezoneOffset() / 60 * -1
  66. return handleCustomTemplate(date, formats, match, (zoneHours >= 0 ? '+' : '-') + padStart(zoneHours, 2, '0') + (length === 1 ? ':' : '') + '00')
  67. }
  68. var fW = function (match, length) {
  69. return padStart(handleCustomTemplate(date, formats, match, getYearWeek(date, (options ? options.firstDay : null) || setupDefaults.firstDayOfWeek)), length, '0')
  70. }
  71. var fD = function (match, length) {
  72. return padStart(handleCustomTemplate(date, formats, match, getYearDay(date)), length, '0')
  73. }
  74. var parseDates = {
  75. yyyy: fy,
  76. yy: fy,
  77. MM: fM,
  78. M: fM,
  79. dd: fd,
  80. d: fd,
  81. HH: fH,
  82. H: fH,
  83. hh: fh,
  84. h: fh,
  85. mm: fm,
  86. m: fm,
  87. ss: fs,
  88. s: fs,
  89. SSS: fS,
  90. S: fS,
  91. ZZ: fZ,
  92. Z: fZ,
  93. WW: fW,
  94. W: fW,
  95. DDD: fD,
  96. D: fD,
  97. a: function (match) {
  98. return handleCustomTemplate(date, formats, match, apm)
  99. },
  100. A: function (match) {
  101. return handleCustomTemplate(date, formats, match, helperStringUpperCase(apm))
  102. },
  103. e: function (match) {
  104. return handleCustomTemplate(date, formats, match, date.getDay())
  105. },
  106. E: function (match) {
  107. return handleCustomTemplate(date, formats, match, date.getDay())
  108. },
  109. q: function (match) {
  110. return handleCustomTemplate(date, formats, match, Math.floor((helperGetDateMonth(date) + 3) / 3))
  111. }
  112. }
  113. return result.replace(dateFormatRE, function (match, skip) {
  114. return skip || (parseDates[match] ? parseDates[match](match, match.length) : match)
  115. })
  116. }
  117. return 'Invalid Date'
  118. }
  119. return ''
  120. }
  121. module.exports = toDateString