dateFormat.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. export function dateFormat(date, format = 'YYYY-MM-DD HH:mm:ss'){
  2. const config = {
  3. YYYY: date.getFullYear(),
  4. MM: date.getMonth()<9?'0'+Number(date.getMonth()+1):Number(date.getMonth()+1),
  5. DD: date.getDate()<10?'0'+date.getDate():date.getDate(),
  6. HH: date.getHours()<10?'0'+date.getHours():date.getHours(),
  7. mm: date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes(),
  8. ss: date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds(),
  9. }
  10. for (const key in config) {
  11. format = format.replace(key, config[key])
  12. }
  13. return format
  14. }
  15. export function unitConvert(num) {
  16. var moneyUnits = ["元", "万元", "亿元", "万亿"]
  17. var dividend = 10000;
  18. var curentNum = Number(num);
  19. //转换数字
  20. var curentUnit = moneyUnits[0];
  21. //转换单位
  22. for (var i = 0; i <4; i++) {
  23. curentUnit = moneyUnits[i]
  24. if(strNumSize(curentNum)<5){
  25. break;
  26. }
  27. curentNum = curentNum / dividend
  28. }
  29. var m = {num: 0, unit: ""}
  30. m.num = curentNum.toFixed(2)
  31. m.unit = curentUnit;
  32. return m;
  33. }
  34. export function strNumSize(tempNum){
  35. var stringNum = tempNum.toString()
  36. var index = stringNum.indexOf(".")
  37. var newNum = stringNum;
  38. if(index!=-1){
  39. newNum = stringNum.substring(0,index)
  40. }
  41. return newNum.length
  42. }