date.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //formatDate(date, "yyyy-MM-dd hh:mm:ss")
  2. // 使用方法
  3. export function formatDate(date, fmt) {
  4. if (/(y+)/.test(fmt)) {
  5. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  6. }
  7. let o = {
  8. 'M+': date.getMonth() + 1,
  9. 'd+': date.getDate(),
  10. 'h+': date.getHours(),
  11. 'm+': date.getMinutes(),
  12. 's+': date.getSeconds()
  13. }
  14. for (let k in o) {
  15. if (new RegExp(`(${k})`).test(fmt)) {
  16. let str = o[k] + ''
  17. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str))
  18. }
  19. }
  20. return fmt
  21. }
  22. function padLeftZero(str) {
  23. return ('00' + str).substr(str.length)
  24. }
  25. export function showTime() {
  26. let show_day = new Array('星期一','星期二','星期三','星期四','星期五','星期六','星期日')
  27. let time = new Date()
  28. let year = time.getFullYear()
  29. let month = time.getMonth() + 1
  30. let date = time.getDate()
  31. let day = time.getDay()
  32. let hour = time.getHours()
  33. let minutes = time.getMinutes()
  34. let second = time.getSeconds()
  35. month < 10 ? month = '0' + month : month
  36. hour < 10 ? hour = '0' + hour : hour
  37. minutes < 10 ? minutes = '0' + minutes : minutes
  38. second < 10 ? second = '0' + second : second;
  39. return year+'年 '+month+'月 '+date+'日 '+' '+show_day[day-1]+' '+hour+':'+minutes+':'+second
  40. }