DateTime.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * 根据date类型返回相应格式日期时间
  3. */
  4. const getComDate = (date,dateType) =>{
  5. //null,ymdhms,yymmdd,yy-mm-dd,yy/mm/dd,yy,mm,dd,hhmmss
  6. const year = date.getFullYear()
  7. const month = date.getMonth() + 1
  8. const day = date.getDate()
  9. const hour = date.getHours()
  10. const minute = date.getMinutes()
  11. const second = date.getSeconds()
  12. // console.log(year + ',' + month + ',' + day + ',' + hour + ',' + minute + ','+second)
  13. var returnDate
  14. if (dateType == 'null'){
  15. returnDate = year + '年' + month + '月' + day + '日' + [hour, minute, second].map(formatNumber).join(':')
  16. }
  17. else if (dateType == 'ymdhms'){
  18. returnDate = [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  19. } else if (dateType == 'yymmdd') {
  20. returnDate = [year, month, day].map(formatNumber).join('')
  21. } else if (dateType == 'yy-mm-dd') {
  22. returnDate = [year, month, day].map(formatNumber).join('-')
  23. } else if (dateType == 'yy') {
  24. returnDate = year
  25. }else if (dateType == 'mm') {
  26. returnDate = month
  27. } else if (dateType == 'dd') {
  28. returnDate = day
  29. } else if (dateType == 'hhmmss'){
  30. returnDate = [hour, minute, second].map(formatNumber).join(':')
  31. } else if (dateType == 'yy/mm/dd hh:mm:ss'){
  32. returnDate = [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  33. }else if(dateType == 'yy-mm'){
  34. returnDate = [year, month].map(formatNumber).join('-')
  35. }
  36. return returnDate
  37. }
  38. const formatNumber = n => {
  39. n = n.toString()
  40. return n[1] ? n : '0' + n
  41. }
  42. module.exports = {
  43. getComDate: getComDate
  44. }