util.js 1.1 KB

1234567891011121314151617181920212223242526
  1. const formatTime = (time, type) => {
  2. var date = new Date(time);
  3. var format = type || "YYYY-MM-DD HH:NN:SS";
  4. const year = date.getFullYear();
  5. const month =
  6. date.getMonth() + 1 < 10
  7. ? "0" + (date.getMonth() + 1)
  8. : date.getMonth() + 1;
  9. const day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  10. const hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  11. const minute =
  12. date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  13. const second =
  14. date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  15. format.indexOf("YYYY") > -1 ? (format = format.replace("YYYY", year)) : "";
  16. format.indexOf("MM") > -1 ? (format = format.replace("MM", month)) : "";
  17. format.indexOf("DD") > -1 ? (format = format.replace("DD", day)) : "";
  18. format.indexOf("HH") > -1 ? (format = format.replace("HH", hour)) : "";
  19. format.indexOf("NN") > -1 ? (format = format.replace("NN", minute)) : "";
  20. format.indexOf("SS") > -1 ? (format = format.replace("SS", second)) : "";
  21. return format;
  22. }
  23. module.exports = {
  24. formatTime
  25. }