utils.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // util.js
  2. const formatTime = date => {
  3. const year = date.getFullYear()
  4. const month = date.getMonth() + 1
  5. const day = date.getDate()
  6. const hour = date.getHours()
  7. const minute = date.getMinutes()
  8. const second = date.getSeconds()
  9. return year+"年"+month+"月"+day+"日"
  10. //返回年月日,时分秒
  11. // return [year, month, day].map(formatNumber)//.join(\'/\') + \' \' + [hour, minute].map(formatNumber).join(\':\')
  12. }
  13. function formatDate(date, fmt) {
  14. if (typeof date == 'string') {
  15. return date;
  16. }
  17. if (!fmt) fmt = "yyyy-MM-dd hh:mm:ss";
  18. if (!date || date == null) return null;
  19. var o = {
  20. 'M+': date.getMonth() + 1, // 月份
  21. 'd+': date.getDate(), // 日
  22. 'h+': date.getHours(), // 小时
  23. 'm+': date.getMinutes(), // 分
  24. 's+': date.getSeconds(), // 秒
  25. 'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
  26. 'S': date.getMilliseconds() // 毫秒
  27. }
  28. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  29. for (var k in o) {
  30. if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  31. }
  32. return fmt
  33. }
  34. module.exports = {
  35. formatTime: formatTime,
  36. formatDate: formatDate
  37. }