utils.js 736 B

1234567891011121314151617181920212223
  1. export function formatDate(date, fmt) {
  2. if (!(date instanceof Date)) {
  3. date = new Date(date)
  4. }
  5. const o = {
  6. 'M+': date.getMonth() + 1,
  7. 'd+': date.getDate(),
  8. 'h+': date.getHours(),
  9. 'm+': date.getMinutes(),
  10. 's+': date.getSeconds(),
  11. 'q+': Math.floor((date.getMonth() + 3) / 3),
  12. 'S': date.getMilliseconds(),
  13. }
  14. if (/(y+)/.test(fmt)) {
  15. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  16. }
  17. for (const k in o) {
  18. if (new RegExp(`(${k})`).test(fmt)) {
  19. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  20. }
  21. }
  22. return fmt
  23. }