date.js 828 B

12345678910111213141516171819202122232425262728293031
  1. module.exports = {
  2. dateFormat: function (date, fmt) {
  3. fmt = fmt || 'yyyy-MM-dd hh:mm:ss'
  4. const o = {
  5. 'M+': date.getMonth() + 1, //月份
  6. 'd+': date.getDate(), //日
  7. 'h+': date.getHours(), //小时
  8. 'm+': date.getMinutes(), //分
  9. 's+': date.getSeconds(), //秒
  10. 'q+': Math.floor((date.getMonth() + 3) / 3), //季度
  11. S: date.getMilliseconds(), //毫秒
  12. }
  13. if (/(y+)/.test(fmt)) {
  14. fmt = fmt.replace(
  15. RegExp.$1,
  16. (date.getFullYear() + '').substr(4 - RegExp.$1.length)
  17. )
  18. }
  19. for (var k in o) {
  20. if (new RegExp('(' + k + ')').test(fmt)) {
  21. fmt = fmt.replace(
  22. RegExp.$1,
  23. RegExp.$1.length == 1
  24. ? o[k]
  25. : ('00' + o[k]).substr(('' + o[k]).length)
  26. )
  27. }
  28. }
  29. return fmt
  30. },
  31. }