date.js 814 B

12345678910111213141516171819202122232425
  1. /*
  2. * @Description: 时间数据格式
  3. * @Autor: kindring
  4. * @Date: 2021-10-14 18:53:00
  5. * @LastEditors: kindring
  6. * @LastEditTime: 2021-10-14 19:08:37
  7. * @LastDescript:
  8. */
  9. function dateFormat(date, format = 'YYYY-MM-DD HH:mm:ss') {
  10. const config = {
  11. YYYY: date.getFullYear(),
  12. MM: ('0' + (date.getMonth() + 1)).substr(-2, 2), //getMonth() 方法根据本地时间返回指定日期的月份(从 0 到 11)
  13. DD: ('0' + date.getDate()).substr(-2, 2),
  14. HH: ('0' + date.getHours()).substr(-2, 2),
  15. mm: ('0' + date.getMinutes()).substr(-2, 2),
  16. ss: ('0' + date.getSeconds()).substr(-2, 2),
  17. }
  18. console.log(config);
  19. for (const key in config) {
  20. format = format.replace(key, config[key])
  21. }
  22. return format
  23. }
  24. console.log(dateFormat(new Date()));