utils.js 735 B

123456789101112131415161718192021222324252627282930313233
  1. function formatTime(format) {
  2. if (format === undefined) format = 'yyyy-MM-dd hh:mm:ss';
  3. const date = new Date();
  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. S: date.getMilliseconds(),
  11. };
  12. if (/(y+)/.test(format)) {
  13. format = format.replace(
  14. RegExp.$1,
  15. (date.getFullYear() + '').substr(4 - RegExp.$1.length)
  16. );
  17. }
  18. for (let k in o) {
  19. if (new RegExp('(' + k + ')').test(format)) {
  20. format = format.replace(
  21. RegExp.$1,
  22. RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
  23. );
  24. }
  25. }
  26. return format;
  27. }
  28. module.exports = {
  29. formatTime,
  30. };