publicFunction.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //1.格式化日期
  2. //time 日期字符串 type 从什么时候开始返回 year = 返回年月日 month = 返回月日..
  3. //style 样式,比如 年-月-日 年.月.日
  4. const getTime = function(time:string,type:string,style:Number){
  5. const date = new Date(time);
  6. const year = date.getFullYear();
  7. const month = date.getMonth() + 1;
  8. const day = date.getDate();
  9. //返回 年-月-日
  10. if(type == 'year'&& style == 1){
  11. return `${year}-${month}-${day}`;
  12. }
  13. //返回 年-月
  14. if(type == 'year'&& style == 2){
  15. return `${year}-${month}`;
  16. }
  17. //返回 年
  18. if(type == 'year'&& style == 3){
  19. return `${year}`;
  20. }
  21. //返回 月-日
  22. if(type == 'month'&& style == 1){
  23. return `${month}-${day}`;
  24. }
  25. //返回 月.日
  26. if(type == 'month'&& style == 2){
  27. return `${month}.${day}`;
  28. }
  29. //返回 日
  30. if(type == 'day'&& style == 1){
  31. return `${day}`;
  32. }
  33. }
  34. //2.格式化标题长度
  35. //title 标题 length 长度
  36. const getTitleLength = function(title:string,length:number){
  37. if(title.length >= length){
  38. return title.substring(0, length) + "...";
  39. }else{
  40. return title;
  41. }
  42. }
  43. export { getTime,getTitleLength };