index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /**
  2. * 获取当前日期
  3. */
  4. export function dateTimeStr(str){
  5. var date = new Date(),
  6. year = date.getFullYear(), //年
  7. month = date.getMonth() + 1, //月
  8. day = date.getDate(), //日
  9. hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(), //时
  10. minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(), //分
  11. second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); //秒
  12. month >= 1 && month <= 9 ? (month = "0" + month) : "";
  13. day >= 0 && day <= 9 ? (day = "0" + day) : "";
  14. hour >= 0 && hour <= 9 ? (hour = "0" + hour) : "";
  15. minute >= 0 && minute <= 9 ? (minute = "0" + minute) : "";
  16. second >= 0 && second <= 9 ? (second = "0" + second) : "";
  17. if(str.indexOf('y') != -1){
  18. str = str.replace('y', year)
  19. }
  20. if(str.indexOf('m') != -1){
  21. str = str.replace('m', month)
  22. }
  23. if(str.indexOf('d') != -1){
  24. str = str.replace('d', day)
  25. }
  26. if(str.indexOf('h') != -1){
  27. str = str.replace('h', hour)
  28. }
  29. if(str.indexOf('i') != -1){
  30. str = str.replace('i', minute)
  31. }
  32. if(str.indexOf('s') != -1){
  33. str = str.replace('s', second)
  34. }
  35. return str;
  36. }