dateFormat.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. // 获取当月最后一天
  16. export function onthelastDay(date,onthelast, format = 'YYYY-MM-DD HH:mm:ss') {
  17. let config = {}
  18. config.YYYY = date.getFullYear()
  19. config.MM = date.getMonth()
  20. config.HH = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  21. config.mm = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  22. config.ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  23. // 获取下个月的第一天
  24. const nextMonth = new Date(config.YYYY, config.MM + 1, 1);
  25. // 获取当前月的最后一天
  26. const lastDay = new Date(nextMonth.getTime() - 86400000);
  27. config.DD = lastDay.getDate() < 10 ? '0' + lastDay.getDate() : lastDay.getDate()
  28. config.MM = config.MM + 1 < 10 ? '0' + (config.MM + 1):config.MM + 1
  29. for (const key in config) {
  30. format = format.replace(key, config[key])
  31. }
  32. return format
  33. }
  34. export function unitConvert(num) {
  35. var moneyUnits = ["元", "万元", "亿元", "万亿"]
  36. var dividend = 10000;
  37. var curentNum = Number(num);
  38. //转换数字
  39. var curentUnit = moneyUnits[0];
  40. //转换单位
  41. for (var i = 0; i < 4; i++) {
  42. curentUnit = moneyUnits[i]
  43. if (strNumSize(curentNum) < 5) {
  44. break;
  45. }
  46. curentNum = curentNum / dividend
  47. }
  48. var m = {
  49. num: 0,
  50. unit: ""
  51. }
  52. m.num = curentNum.toFixed(2)
  53. m.unit = curentUnit;
  54. return m;
  55. }
  56. export function DX(n) {
  57. if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n))
  58. return "数据非法";
  59. var unit = "千百拾亿千百拾万千百拾元角分",
  60. str = "";
  61. n += "00";
  62. var p = n.indexOf('.');
  63. if (p >= 0)
  64. n = n.substring(0, p) + n.substr(p + 1, 2);
  65. unit = unit.substr(unit.length - n.length);
  66. for (var i = 0; i < n.length; i++)
  67. str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);
  68. return str.replace(/零(千|百|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g,
  69. "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整");
  70. }
  71. export function strNumSize(tempNum) {
  72. var stringNum = tempNum.toString()
  73. var index = stringNum.indexOf(".")
  74. var newNum = stringNum;
  75. if (index != -1) {
  76. newNum = stringNum.substring(0, index)
  77. }
  78. return newNum.length
  79. }