dateFormat.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 = {
  30. num: 0,
  31. unit: ""
  32. }
  33. m.num = curentNum.toFixed(2)
  34. m.unit = curentUnit;
  35. return m;
  36. }
  37. export function DX(n) {
  38. if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n))
  39. return "数据非法";
  40. var unit = "千百拾亿千百拾万千百拾元角分",
  41. str = "";
  42. n += "00";
  43. var p = n.indexOf('.');
  44. if (p >= 0)
  45. n = n.substring(0, p) + n.substr(p + 1, 2);
  46. unit = unit.substr(unit.length - n.length);
  47. for (var i = 0; i < n.length; i++)
  48. str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);
  49. return str.replace(/零(千|百|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g,
  50. "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整");
  51. }
  52. export function strNumSize(tempNum) {
  53. var stringNum = tempNum.toString()
  54. var index = stringNum.indexOf(".")
  55. var newNum = stringNum;
  56. if (index != -1) {
  57. newNum = stringNum.substring(0, index)
  58. }
  59. return newNum.length
  60. }