format.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. export function debounce(func, wait) {
  2. let timeout;
  3. return function () {
  4. let context = this;
  5. let args = arguments;
  6. if (timeout) clearTimeout(timeout);
  7. let callNow = !timeout;
  8. timeout = setTimeout(() => {
  9. timeout = null;
  10. }, wait)
  11. if (callNow) func.apply(context, args)
  12. }
  13. }
  14. // 获取出生日期
  15. export function getBirth(idCard) {
  16. if (!idCard) return
  17. var birthday = "";
  18. if (idCard != null && idCard != "") {
  19. if (idCard.length == 15) {
  20. birthday = "19" + idCard.slice(6, 12);
  21. } else if (idCard.length == 18) {
  22. birthday = idCard.slice(6, 14);
  23. }
  24. birthday = birthday.replace(/(.{4})(.{2})/, "$1-$2-");
  25. //通过正则表达式来指定输出格式为:1990-01-01
  26. }
  27. return birthday;
  28. }
  29. // 通过身份证号获取性别
  30. export function getSex(idCard) {
  31. if (!idCard) return
  32. var sexStr = '';
  33. if (parseInt(idCard.slice(-2, -1)) % 2 == 1) {
  34. sexStr = '男';
  35. } else {
  36. sexStr = '女';
  37. }
  38. return sexStr;
  39. }
  40. // 转换 类似 2 => 二
  41. export function numberToUpperCase(textIndex) {
  42. let newString = '';
  43. let newTextIndex = (textIndex + 1) + '';
  44. function sum(value, index) {
  45. var newValue = '';
  46. if ((textIndex === 9)) {
  47. return !index ? '十' : '';
  48. }
  49. let isSeat = (~~textIndex > 9 && ~~textIndex < 19);
  50. switch (~~value) {
  51. case 1:
  52. newValue = !index ? (isSeat ? '' : '一') : '十一';
  53. break;
  54. case 2:
  55. newValue = !index ? (isSeat ? '' : '二') : '十二';
  56. break;
  57. case 3:
  58. newValue = !index ? (isSeat ? '' : '三') : '十三';
  59. break;
  60. case 4:
  61. newValue = !index ? (isSeat ? '' : '四') : '十四';
  62. break;
  63. case 5:
  64. newValue = !index ? (isSeat ? '' : '五') : '十五';
  65. break;
  66. case 6:
  67. newValue = !index ? (isSeat ? '' : '六') : '十六';
  68. break;
  69. case 7:
  70. newValue = !index ? (isSeat ? '' : '七') : '十七';
  71. break;
  72. case 8:
  73. newValue = !index ? (isSeat ? '' : '八') : '十八';
  74. break;
  75. case 9:
  76. newValue = !index ? (isSeat ? '' : '九') : '九十';
  77. break;
  78. case 0:
  79. newValue = '十';
  80. break;
  81. default:
  82. break;
  83. }
  84. return newValue;
  85. }
  86. for (let i = 0; i < newTextIndex.length; i++) {
  87. newString += sum(newTextIndex.substring(i, i + 1), i);
  88. }
  89. return newString;
  90. }
  91. export function formatMonth(date) {
  92. if (!date) return '';
  93. if (typeof date === 'number') date = new Date(date);
  94. const year = date.getFullYear(); // 获取年份
  95. const month = date.getMonth() + 1; // 获取月份,月份是从0开始的,所以需要加1
  96. const formattedMonth = month < 10 ? '0' + month : month; // 如果月份小于10,前面补0
  97. return `${year}-${formattedMonth}`; // 返回格式化后的字符串
  98. }
  99. export function subtractMonths(value, date = new Date()) {
  100. // 获取原始日期的年、月、日
  101. const year = date.getFullYear();
  102. const month = date.getMonth();
  103. const day = date.getDate();
  104. let _month = month - Math.abs(value);
  105. const _year = year + Math.floor((_month - (month + 1)) / 12);
  106. _month = _month - Math.floor((_month - (month + 1)) / 12) * 12;
  107. // 处理月份天数不足的情况,比如1月31日加1个月不能是2月31日
  108. let _date = new Date(_year, _month, day);
  109. if (_date.getMonth() !== _month) {
  110. const _day = Math.min(day, new Date(_year, _month + 1, 0).getDate());
  111. _date = new Date(_year, _month, _day);
  112. }
  113. return _date;
  114. }
  115. export function numberToChinese(num) {
  116. if (num === 0) return "零";
  117. const units = ["", "十", "百", "千"];
  118. const bigUnits = ["", "万", "亿", "兆"];
  119. const digits = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
  120. let result = "";
  121. let section = 0; // 当前处理的节(每四位为一节)
  122. while (num > 0) {
  123. let part = num % 10000; // 取出当前节的四位数字
  124. let partStr = "";
  125. if (part !== 0) {
  126. let unitIndex = 0; // 当前处理的位(个、十、百、千)
  127. while (part > 0) {
  128. let digit = part % 10; // 当前位的数字
  129. if (digit !== 0) {
  130. partStr = digits[digit] + units[unitIndex] + partStr;
  131. } else if (!partStr.startsWith(digits[0])) {
  132. partStr = digits[0] + partStr;
  133. }
  134. part = Math.floor(part / 10);
  135. unitIndex++;
  136. }
  137. }
  138. if (partStr !== "") {
  139. partStr += bigUnits[section];
  140. result = partStr + result;
  141. } else if (result !== "" && !result.startsWith(digits[0])) {
  142. result = digits[0] + result;
  143. }
  144. num = Math.floor(num / 10000);
  145. section++;
  146. }
  147. // 处理特殊情况:连续的零
  148. result = result.replace(/零{2,}/g, "零");
  149. // 处理特殊情况:末尾的零
  150. if (result.endsWith("零")) {
  151. result = result.slice(0, -1);
  152. }
  153. return result;
  154. }