toDateString.js 4.6 KB

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