toStringDate.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. var setupDefaults = require('./setupDefaults')
  2. var staticParseInt = require('./staticParseInt')
  3. var helperGetUTCDateTime = require('./helperGetUTCDateTime')
  4. var helperGetDateTime = require('./helperGetDateTime')
  5. var isString = require('./isString')
  6. var isDate = require('./isDate')
  7. var dateFormatRules = [
  8. { rules: [['yyyy', 4]] },
  9. { rules: [['MM', 2], ['M', 1]], offset: -1 },
  10. { rules: [['dd', 2], ['d', 1]] },
  11. { rules: [['HH', 2], ['H', 1]] },
  12. { rules: [['mm', 2], ['m', 1]] },
  13. { rules: [['ss', 2], ['s', 1]] },
  14. { rules: [['SSS', 3], ['S', 1]] },
  15. { rules: [['ZZ', 5], ['Z', 6], ['Z', 5], ['Z', 1]] }
  16. ]
  17. function parseStringDate (str, format) {
  18. var arr, sIndex, fIndex, fLen, fItem, rules, rIndex, rLen, tempMatch
  19. var dates = [0, 0, 1, 0, 0, 0, 0]
  20. for (fIndex = 0, fLen = dateFormatRules.length; fIndex < fLen; fIndex++) {
  21. fItem = dateFormatRules[fIndex]
  22. for (rIndex = 0, rules = fItem.rules, rLen = rules.length; rIndex < rLen; rIndex++) {
  23. arr = rules[rIndex]
  24. sIndex = format.indexOf(arr[0])
  25. if (sIndex > -1) {
  26. tempMatch = str.substring(sIndex, sIndex + arr[1])
  27. if (tempMatch && tempMatch.length === arr[1]) {
  28. if (fItem.offset) {
  29. tempMatch = staticParseInt(tempMatch) + fItem.offset
  30. }
  31. dates[fIndex] = tempMatch
  32. break
  33. }
  34. }
  35. if (rIndex === rLen - 1) {
  36. return dates
  37. }
  38. }
  39. }
  40. return dates
  41. }
  42. /**
  43. * 字符串转为日期
  44. *
  45. * @param {String/Number/Date} str 日期或数字
  46. * @param {String} format 解析日期格式(yyyy年份、MM月份、dd天、hh(12)HH(24)小时、mm分钟、ss秒、SSS毫秒、Z时区)
  47. * @return {Date}
  48. */
  49. function toStringDate (str, format) {
  50. var rest, isDType
  51. if (str) {
  52. isDType = isDate(str)
  53. if (isDType || (!format && /^[0-9]{11,15}$/.test(str))) {
  54. rest = new Date(isDType ? helperGetDateTime(str) : staticParseInt(str))
  55. } else if (isString(str)) {
  56. var tempMatch
  57. var dates = parseStringDate(str, format || setupDefaults.formatDate)
  58. var zStr = dates[7]
  59. if (dates[0]) {
  60. // 解析时区
  61. if (zStr) {
  62. // 如果为UTC 时间
  63. if (zStr[0] === 'z' || zStr[0] === 'Z') {
  64. rest = new Date(helperGetUTCDateTime(dates))
  65. } else {
  66. // 如果指定时区,时区转换
  67. tempMatch = zStr.match(/([-+]{1})(\d{2}):?(\d{2})/)
  68. if (tempMatch) {
  69. rest = new Date(helperGetUTCDateTime(dates) - (tempMatch[1] === '-' ? -1 : 1) * staticParseInt(tempMatch[2]) * 3600000 + staticParseInt(tempMatch[3]) * 60000)
  70. }
  71. }
  72. } else {
  73. rest = new Date(dates[0], dates[1], dates[2], dates[3], dates[4], dates[5], dates[6])
  74. }
  75. }
  76. }
  77. }
  78. return rest ? rest : new Date('')
  79. }
  80. module.exports = toStringDate