toDateString.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. function formatDayE (day) {
  24. return day === 0 ? 7 : day
  25. }
  26. /**
  27. * 日期格式化为字符串,转义符号 []
  28. *
  29. * @param {Date} date 日期或数字
  30. * @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时区)
  31. * @param {Object} options {formats: {q: ['日', '一', '二', '三', '四', '五', '六'], E: function (value, match, date) {return '三'}, }} 自定义格式化模板
  32. * @return {String}
  33. */
  34. 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
  35. function toDateString (date, format, options) {
  36. if (date) {
  37. date = toStringDate(date)
  38. if (isValidDate(date)) {
  39. var result = format || setupDefaults.formatString
  40. var hours = date.getHours()
  41. var apm = hours < 12 ? 'am' : 'pm'
  42. var formats = assign({}, setupDefaults.formatStringMatchs, options ? options.formats : null)
  43. var fy = function (match, length) {
  44. return ('' + helperGetDateFullYear(date)).substr(4 - length)
  45. }
  46. var fM = function (match, length) {
  47. return padStart(helperGetDateMonth(date) + 1, length, '0')
  48. }
  49. var fd = function (match, length) {
  50. return padStart(date.getDate(), length, '0')
  51. }
  52. var fH = function (match, length) {
  53. return padStart(hours, length, '0')
  54. }
  55. var fh = function (match, length) {
  56. return padStart(hours <= 12 ? hours : hours - 12, length, '0')
  57. }
  58. var fm = function (match, length) {
  59. return padStart(date.getMinutes(), length, '0')
  60. }
  61. var fs = function (match, length) {
  62. return padStart(date.getSeconds(), length, '0')
  63. }
  64. var fS = function (match, length) {
  65. return padStart(date.getMilliseconds(), length, '0')
  66. }
  67. var fZ = function (match, length) {
  68. var zoneHours = date.getTimezoneOffset() / 60 * -1
  69. return handleCustomTemplate(date, formats, match, (zoneHours >= 0 ? '+' : '-') + padStart(zoneHours, 2, '0') + (length === 1 ? ':' : '') + '00')
  70. }
  71. var fW = function (match, length) {
  72. return padStart(handleCustomTemplate(date, formats, match, getYearWeek(date)), length, '0')
  73. }
  74. var fD = function (match, length) {
  75. return padStart(handleCustomTemplate(date, formats, match, getYearDay(date)), length, '0')
  76. }
  77. var parseDates = {
  78. yyyy: fy,
  79. yy: fy,
  80. MM: fM,
  81. M: fM,
  82. dd: fd,
  83. d: fd,
  84. HH: fH,
  85. H: fH,
  86. hh: fh,
  87. h: fh,
  88. mm: fm,
  89. m: fm,
  90. ss: fs,
  91. s: fs,
  92. SSS: fS,
  93. S: fS,
  94. ZZ: fZ,
  95. Z: fZ,
  96. WW: fW,
  97. W: fW,
  98. DDD: fD,
  99. D: fD,
  100. a: function (match) {
  101. return handleCustomTemplate(date, formats, match, apm)
  102. },
  103. A: function (match) {
  104. return handleCustomTemplate(date, formats, match, helperStringUpperCase(apm))
  105. },
  106. e: function (match) {
  107. return handleCustomTemplate(date, formats, match, date.getDay())
  108. },
  109. E: function (match) {
  110. return handleCustomTemplate(date, formats, match, formatDayE(date.getDay()))
  111. },
  112. q: function (match) {
  113. return handleCustomTemplate(date, formats, match, Math.floor((helperGetDateMonth(date) + 3) / 3))
  114. }
  115. }
  116. return result.replace(dateFormatRE, function (match, skip) {
  117. return skip || (parseDates[match] ? parseDates[match](match, match.length) : match)
  118. })
  119. }
  120. return 'Invalid Date'
  121. }
  122. return ''
  123. }
  124. module.exports = toDateString