time-processer.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. module.exports = function getNowFormatDate() {
  2. let date = new Date();
  3. let year = date.getFullYear(); //年 ,从 Date 对象以四位数字返回年份
  4. let month = date.getMonth() + 1; //月 ,从 Date 对象返回月份 (0 ~ 11) ,date.getMonth()比实际月份少 1 个月
  5. let day = date.getDate(); //日 ,从 Date 对象返回一个月中的某一天 (1 ~ 31)
  6. let hours = date.getHours(); //小时 ,返回 Date 对象的小时 (0 ~ 23)
  7. let minutes = date.getMinutes(); //分钟 ,返回 Date 对象的分钟 (0 ~ 59)
  8. let seconds = date.getSeconds(); //秒 ,返回 Date 对象的秒数 (0 ~ 59)
  9. //修改月份格式
  10. if (month >= 1 && month <= 9) {
  11. month = "0" + month;
  12. }
  13. //修改日期格式
  14. if (day >= 0 && day <= 9) {
  15. day = "0" + day;
  16. }
  17. //修改小时格式
  18. if (hours >= 0 && hours <= 9) {
  19. hours = "0" + hours;
  20. }
  21. //修改分钟格式
  22. if (minutes >= 0 && minutes <= 9) {
  23. minutes = "0" + minutes;
  24. }
  25. //修改秒格式
  26. if (seconds >= 0 && seconds <= 9) {
  27. seconds = "0" + seconds;
  28. }
  29. //获取当前系统时间 格式(yyyy-mm-dd hh:mm:ss)
  30. let currentFormatDate = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
  31. return currentFormatDate;
  32. }