utils.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. export const formatTimestamp = t => { //时间戳格式化
  2. if (!t) return ['', '', '', '', '', '', '']
  3. t = (t + '').length === 10 ? `${t}000` : t
  4. const date = new Date(t)
  5. const year = date.getFullYear()
  6. const month = date.getMonth() + 1
  7. const week = date.getDay()
  8. const day = date.getDate()
  9. const hour = date.getHours()
  10. const minutes = date.getMinutes()
  11. const second = date.getSeconds()
  12. let weekRes = ''
  13. switch (week) {
  14. case 0:
  15. weekRes = "星期日";
  16. break;
  17. case 1:
  18. weekRes = "星期一";
  19. break;
  20. case 2:
  21. weekRes = "星期二";
  22. break;
  23. case 3:
  24. weekRes = "星期三";
  25. break;
  26. case 4:
  27. weekRes = "星期四";
  28. break;
  29. case 5:
  30. weekRes = "星期五";
  31. break;
  32. case 6:
  33. weekRes = "星期六";
  34. }
  35. return [year, month, weekRes, day, hour, minutes, second]
  36. }
  37. export const numberToChinese = num => { //数字转汉字方法
  38. const chnNumChar = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
  39. const chnUnitSection = ["", "万", "亿", "万亿", "亿亿"];
  40. const chnUnitChar = ["", "十", "百", "千"];
  41. const numToChn = num => {
  42. const index = num.toString().indexOf(".");
  43. if (index != -1) {
  44. const str = num.toString().slice(index);
  45. let a = "点";
  46. for (let i = 1; i < str.length; i++) {
  47. a += chnNumChar[parseInt(str[i])];
  48. }
  49. return a;
  50. } else {
  51. return '';
  52. }
  53. }
  54. //定义在每个小节的内部进行转化的方法,其他部分则与小节内部转化方法相同
  55. const sectionToChinese = section => {
  56. var str = '',
  57. chnstr = '',
  58. zero = false,
  59. count = 0; //zero为是否进行补零, 第一次进行取余由于为个位数,默认不补零
  60. while (section > 0) {
  61. var v = section % 10; //对数字取余10,得到的数即为个位数
  62. if (v == 0) { //如果数字为零,则对字符串进行补零
  63. if (zero) {
  64. zero = false; //如果遇到连续多次取余都是0,那么只需补一个零即可
  65. chnstr = chnNumChar[v] + chnstr;
  66. }
  67. } else {
  68. zero = true; //第一次取余之后,如果再次取余为零,则需要补零
  69. str = chnNumChar[v];
  70. str += chnUnitChar[count];
  71. chnstr = str + chnstr;
  72. }
  73. count++;
  74. section = Math.floor(section / 10);
  75. }
  76. return chnstr;
  77. }
  78. const a = numToChn(num);
  79. num = Math.floor(num);
  80. let unitPos = 0;
  81. let strIns = '';
  82. let chnStr = '';
  83. let needZero = false;
  84. if (num === 0) {
  85. return chnNumChar[0];
  86. }
  87. while (num > 0) {
  88. let section = num % 10000;
  89. if (needZero) {
  90. chnStr = chnNumChar[0] + chnStr;
  91. }
  92. strIns = sectionToChinese(section);
  93. strIns += (section !== 0) ? chnUnitSection[unitPos] : chnUnitSection[0];
  94. chnStr = strIns + chnStr;
  95. needZero = (section < 1000) && (section > 0);
  96. num = Math.floor(num / 10000);
  97. unitPos++;
  98. }
  99. let result = chnStr + a;
  100. const temp = result.split('')
  101. if (temp[0] === '一' && temp[1] === '十') {
  102. temp.splice(0, 1)
  103. result = temp.join('')
  104. }
  105. return result ? result : ''
  106. }