getTime.js 895 B

12345678910111213141516171819202122232425262728293031
  1. const formatTime = (date = new Date(), j1 = '/', j2 = ':') => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return `${[year, month, day].map(formatNumber).join(j1)} ${[hour, minute, second].map(formatNumber).join(j2)}`
  9. }
  10. const formatNumber = n => {
  11. n = n.toString()
  12. return n[1] ? n : `0${n}`
  13. }
  14. function getWeek(deta = new Date(), prefix = '周') {
  15. weeks = new Array("日", "一", "二", "三", "四", "五", "六");
  16. return prefix + weeks[deta.getDay()];
  17. }
  18. function getYMD(date = new Date()) {
  19. const year = date.getFullYear()
  20. const month = date.getMonth() + 1
  21. const day = date.getDate()
  22. return `${year}年${month}月${day}日`
  23. }
  24. module.exports = {
  25. formatTime,
  26. getWeek,
  27. getYMD
  28. }