date.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. function formatTime(fmt, date1) {
  2. let ret;
  3. let date = new Date(date1);
  4. const opt = {
  5. "Y+": date.getFullYear().toString(),   // 年
  6. "m+": (date.getMonth() + 1).toString(),   // 月
  7. "d+": date.getDate().toString(),   // 日
  8. "H+": date.getHours().toString(),   // 时
  9. "M+": date.getMinutes().toString(),   // 分
  10. "S+": date.getSeconds().toString(),   // 秒
  11. // 有其他格式化字符需求可以继续添加,必须转化成字符串
  12. };
  13. for (let k in opt) { ret = new RegExp("(" + k + ")").exec(fmt); if (ret) { fmt = fmt.replace(ret[1], ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, "0")); } }
  14. return fmt;
  15. }
  16. //获取本季度的开端月份
  17. function getQuarterStartMonth() {
  18. let quarterStartMonth = 0
  19. let nowMonth = new Date().getMonth()
  20. if (nowMonth < 3) {
  21. quarterStartMonth = "01"
  22. }
  23. if (nowMonth > 2 && nowMonth < 6) {
  24. quarterStartMonth = "03"
  25. }
  26. if (nowMonth > 5 && nowMonth < 9) {
  27. quarterStartMonth = "06"
  28. }
  29. if (nowMonth > 8) {
  30. quarterStartMonth = "09"
  31. }
  32. return quarterStartMonth;
  33. }
  34. module.exports = {
  35. formatTime,
  36. getQuarterStartMonth
  37. };