date.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. ## Date
  3. */
  4. var patternLetters = {
  5. yyyy: 'getFullYear',
  6. yy: function(date) {
  7. return ('' + date.getFullYear()).slice(2)
  8. },
  9. y: 'yy',
  10. MM: function(date) {
  11. var m = date.getMonth() + 1
  12. return m < 10 ? '0' + m : m
  13. },
  14. M: function(date) {
  15. return date.getMonth() + 1
  16. },
  17. dd: function(date) {
  18. var d = date.getDate()
  19. return d < 10 ? '0' + d : d
  20. },
  21. d: 'getDate',
  22. HH: function(date) {
  23. var h = date.getHours()
  24. return h < 10 ? '0' + h : h
  25. },
  26. H: 'getHours',
  27. hh: function(date) {
  28. var h = date.getHours() % 12
  29. return h < 10 ? '0' + h : h
  30. },
  31. h: function(date) {
  32. return date.getHours() % 12
  33. },
  34. mm: function(date) {
  35. var m = date.getMinutes()
  36. return m < 10 ? '0' + m : m
  37. },
  38. m: 'getMinutes',
  39. ss: function(date) {
  40. var s = date.getSeconds()
  41. return s < 10 ? '0' + s : s
  42. },
  43. s: 'getSeconds',
  44. SS: function(date) {
  45. var ms = date.getMilliseconds()
  46. return ms < 10 && '00' + ms || ms < 100 && '0' + ms || ms
  47. },
  48. S: 'getMilliseconds',
  49. A: function(date) {
  50. return date.getHours() < 12 ? 'AM' : 'PM'
  51. },
  52. a: function(date) {
  53. return date.getHours() < 12 ? 'am' : 'pm'
  54. },
  55. T: 'getTime'
  56. }
  57. module.exports = {
  58. // 日期占位符集合。
  59. _patternLetters: patternLetters,
  60. // 日期占位符正则。
  61. _rformat: new RegExp((function() {
  62. var re = []
  63. for (var i in patternLetters) re.push(i)
  64. return '(' + re.join('|') + ')'
  65. })(), 'g'),
  66. // 格式化日期。
  67. _formatDate: function(date, format) {
  68. return format.replace(this._rformat, function creatNewSubString($0, flag) {
  69. return typeof patternLetters[flag] === 'function' ? patternLetters[flag](date) :
  70. patternLetters[flag] in patternLetters ? creatNewSubString($0, patternLetters[flag]) :
  71. date[patternLetters[flag]]()
  72. })
  73. },
  74. // 生成一个随机的 Date 对象。
  75. _randomDate: function(min, max) { // min, max
  76. min = min === undefined ? new Date(0) : min
  77. max = max === undefined ? new Date() : max
  78. return new Date(Math.random() * (max.getTime() - min.getTime()))
  79. },
  80. // 返回一个随机的日期字符串。
  81. date: function(format) {
  82. format = format || 'yyyy-MM-dd'
  83. return this._formatDate(this._randomDate(), format)
  84. },
  85. // 返回一个随机的时间字符串。
  86. time: function(format) {
  87. format = format || 'HH:mm:ss'
  88. return this._formatDate(this._randomDate(), format)
  89. },
  90. // 返回一个随机的日期和时间字符串。
  91. datetime: function(format) {
  92. format = format || 'yyyy-MM-dd HH:mm:ss'
  93. return this._formatDate(this._randomDate(), format)
  94. },
  95. // 返回当前的日期和时间字符串。
  96. now: function(unit, format) {
  97. // now(unit) now(format)
  98. if (arguments.length === 1) {
  99. // now(format)
  100. if (!/year|month|day|hour|minute|second|week/.test(unit)) {
  101. format = unit
  102. unit = ''
  103. }
  104. }
  105. unit = (unit || '').toLowerCase()
  106. format = format || 'yyyy-MM-dd HH:mm:ss'
  107. var date = new Date()
  108. /* jshint -W086 */
  109. // 参考自 http://momentjs.cn/docs/#/manipulating/start-of/
  110. switch (unit) {
  111. case 'year':
  112. date.setMonth(0)
  113. case 'month':
  114. date.setDate(1)
  115. case 'week':
  116. case 'day':
  117. date.setHours(0)
  118. case 'hour':
  119. date.setMinutes(0)
  120. case 'minute':
  121. date.setSeconds(0)
  122. case 'second':
  123. date.setMilliseconds(0)
  124. }
  125. switch (unit) {
  126. case 'week':
  127. date.setDate(date.getDate() - date.getDay())
  128. }
  129. return this._formatDate(date, format)
  130. }
  131. }