filter.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // 金额转化 2001=>20.01
  2. const conversion = (num : number) => {
  3. let str = (num / 100).toFixed(2) + '';
  4. let intSum = str.substring(0, str.indexOf('.')).replace(/\B(?=(?:\d{3})+$)/g, ',');//取到整数部分
  5. let dot = str.substring(str.length, str.indexOf('.'));//取到小数部分搜索
  6. let ret = intSum + dot;
  7. return ret;
  8. };
  9. const getMonthTimestamps = (month : string) => {
  10. //解析月份字符串
  11. const [yearstr, monthStr] = month.split('-');
  12. const year = parseInt(yearstr);
  13. const monthIndex = parseInt(monthStr) - 1; // 月份从 开始计数,所以需
  14. //获取该月份开始的时间戳
  15. const startofMonth = new Date(year, monthIndex, 1).getTime();
  16. // 获取下个月份开始的时间戳
  17. const nextMonth = monthIndex === 11 ? 0 : monthIndex + 1;
  18. const nextYear = monthIndex === 11 ? year + 1 : year;
  19. const startofNextMonth = new Date(nextYear, nextMonth, 1).getTime();
  20. return [
  21. startofMonth,
  22. startofNextMonth
  23. ]
  24. }
  25. function timestampToDatetime(timestamp : number, format = 'YYYY-MM-DD hh:mm:ss') {
  26. if(timestamp == 0) {
  27. return '-'
  28. }
  29. // 创建一个新的Date对象,使用传入的时间戳作为参数
  30. const date = new Date(timestamp);
  31. // 获取年、月、日、时、分、秒
  32. const year = date.getFullYear();
  33. const month = ('0' + (date.getMonth() + 1)).slice(-2); // 月份是从0开始的,所以要加1
  34. const day = ('0' + date.getDate()).slice(-2);
  35. const hours = ('0' + date.getHours()).slice(-2);
  36. const minutes = ('0' + date.getMinutes()).slice(-2);
  37. const seconds = ('0' + date.getSeconds()).slice(-2);
  38. if (format == 'YYYY-MM-DD') {
  39. return year + '-' + month + '-' + day
  40. }
  41. // 返回格式化后的时间字符串
  42. return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
  43. }
  44. function getWxMiniProgramUrlParam(url) {
  45. let theRequest = {};
  46. if (url.indexOf("#") != -1) {
  47. const str = url.split("#")[1];
  48. const strs = str.split("&");
  49. for (let i = 0; i < strs.length; i++) {
  50. theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
  51. }
  52. } else if (url.indexOf("?") != -1) {
  53. const str = url.split("?")[1];
  54. const strs = str.split("&");
  55. for (let i = 0; i < strs.length; i++) {
  56. theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
  57. }
  58. }
  59. return theRequest;
  60. }
  61. export {
  62. conversion,
  63. getMonthTimestamps,
  64. timestampToDatetime,
  65. getWxMiniProgramUrlParam
  66. }