b39b276c732ce0cca16e0582c23103f88a5605c5.svn-base 798 B

12345678910111213141516171819202122232425262728293031323334353637
  1. const dateFormat = (date, block) => {
  2. if (!date) {
  3. return ''
  4. }
  5. let format = block || 'yyyy-MM-dd'
  6. date = new Date(date)
  7. const map = {
  8. M: date.getMonth() + 1, // 月份
  9. d: date.getDate(), // 日
  10. h: date.getHours(), // 小时
  11. m: date.getMinutes(), // 分
  12. s: date.getSeconds(), // 秒
  13. q: Math.floor((date.getMonth() + 3) / 3), // 季度
  14. S: date.getMilliseconds() // 毫秒
  15. }
  16. format = format.replace(/([yMdhmsqS])+/g, (all, t) => {
  17. let v = map[t]
  18. if (v !== undefined) {
  19. if (all.length > 1) {
  20. v = `0${v}`
  21. v = v.substr(v.length - 2)
  22. }
  23. return v
  24. } else if (t === 'y') {
  25. return (date.getFullYear().toString()).substr(4 - all.length)
  26. }
  27. return all
  28. })
  29. return format
  30. }
  31. export default dateFormat