date.js 1002 B

123456789101112131415161718192021222324252627282930
  1. /*
  2. * @Description: 时间数据格式
  3. * @Autor: kindring
  4. * @Date: 2021-10-14 18:53:00
  5. * @LastEditors: kindring
  6. * @LastEditTime: 2022-03-10 16:34:56
  7. * @LastDescript:
  8. */
  9. /**
  10. * 字符串格式化,自动填充0 不限大小写
  11. * @param {*} date 时间对象
  12. * @param {*} format 需要转换的格式 YYYY 年 MM月 DD 日 H小时 m分钟 s秒
  13. * @returns
  14. */
  15. function dateFormat(date, format = 'YYYY-MM-DD H:m:s') {
  16. format = format.toLocaleUpperCase();
  17. const config = {
  18. YYYY: date.getFullYear(),
  19. MM: ('0' + (date.getMonth() + 1)).substr(-2, 2), //getMonth() 方法根据本地时间返回指定日期的月份(从 0 到 11)
  20. DD: ('0' + date.getDate()).substr(-2, 2),
  21. H: ('0' + date.getHours()).substr(-2, 2),
  22. M: ('0' + date.getMinutes()).substr(-2, 2),
  23. S: ('0' + date.getSeconds()).substr(-2, 2),
  24. }
  25. console.log(config);
  26. for (const key in config) {
  27. format = format.replace(key, config[key])
  28. }
  29. return format
  30. }