index.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // 下载
  2. export function downloadFile (url) {
  3. if (typeof window !== "undefined") {
  4. const userAgent = navigator.userAgent;
  5. if (userAgent.indexOf("Android") > -1) {
  6. window.open(url);
  7. } else if (
  8. userAgent.indexOf("iPhone") > -1 ||
  9. userAgent.indexOf("iPad") > -1 ||
  10. userAgent.indexOf("iPod") > -1
  11. ) {
  12. location.href = url;
  13. } else {
  14. window.open(url);
  15. }
  16. }
  17. }
  18. // js日期格式化(xxxx年xx月xx日)
  19. export function dateFomat (dateStr: String) {
  20. var result = "";
  21. if (dateStr != "") {
  22. var contractdate = new Date(dateStr);
  23. var contract_year = contractdate.getFullYear();
  24. var contract_month = (contractdate.getMonth() + 1) < 10 ? '0' + (contractdate.getMonth() + 1) : (contractdate.getMonth() + 1);
  25. var contract_day = contractdate.getDate() < 10 ? '0' + contractdate.getDate() : contractdate.getDate();
  26. result = contract_year + '年' + contract_month + '月' + contract_day + '日';
  27. }
  28. return result;
  29. }
  30. // 获取标签中内容
  31. export function getStringContent (html: String, tagName: String) {
  32. const regex = new RegExp(`<${tagName}.*?>(.*?)</${tagName}>`, 's'); // 's' 允许 . 匹配换行符
  33. const match = html.match(regex);
  34. return match ? match[1] : null;
  35. }
  36. export function getCookieLang () {
  37. let lang = useCookie('i18n_redirected').value;
  38. return lang
  39. }
  40. /**
  41. * 日期时间格式化未字符串输出
  42. * @param {(Date | string)} date
  43. * @param {string} format
  44. */
  45. export function formatDate (date: Date | string | number, format: string) {
  46. if (!date) {
  47. return "";
  48. }
  49. try {
  50. const value: Date = new Date(date);
  51. const yyyy = value.getFullYear();
  52. const M = value.getMonth() + 1;
  53. const MM =
  54. value.getMonth() + 1 >= 10
  55. ? value.getMonth() + 1
  56. : "0" + (value.getMonth() + 1);
  57. const d = value.getDate();
  58. const dd = value.getDate() >= 10 ? value.getDate() : "0" + value.getDate();
  59. const H = value.getHours();
  60. const HH =
  61. value.getHours() >= 10 ? value.getHours() : "0" + value.getHours();
  62. const m = value.getMinutes();
  63. const mm =
  64. value.getMinutes() >= 10 ? value.getMinutes() : "0" + value.getMinutes();
  65. const s = value.getSeconds();
  66. const ss =
  67. value.getSeconds() >= 10 ? value.getSeconds() : "0" + value.getSeconds();
  68. if (format === "yyyy") {
  69. return yyyy;
  70. } else if (format === "yyyy-MM") {
  71. return yyyy + "-" + MM;
  72. } else if (format === "yyyy-M") {
  73. return yyyy + "-" + M;
  74. } else if (format === "yyyy-MM-dd") {
  75. return yyyy + "-" + MM + "-" + dd;
  76. } else if (format === "yyyy-M-d") {
  77. return yyyy + "-" + M + "-" + d;
  78. } else if (format === "HH:mm:ss") {
  79. return HH + ":" + mm + ":" + ss;
  80. } else if (format === "H:m:s") {
  81. return H + ":" + m + ":" + s;
  82. } else if (format === "HH:mm") {
  83. return HH + ":" + mm;
  84. } else if (format === "H:m") {
  85. return H + ":" + m;
  86. } else if (format === "yyyy-MM-dd HH:mm") {
  87. return yyyy + "-" + MM + "-" + dd + " " + HH + ":" + mm;
  88. } else if (format === "yyyy-MM-dd HH:mm:ss") {
  89. return yyyy + "-" + MM + "-" + dd + " " + HH + ":" + mm + ":" + ss;
  90. } else {
  91. return yyyy + "-" + MM + "-" + dd;
  92. }
  93. } catch (error) {
  94. return "";
  95. }
  96. }
  97. // js日期格式化(May 10, 2025)
  98. export function formatDateEn (inputDate: any) {
  99. let options = { year: 'numeric', month: 'long', day: 'numeric' };
  100. let date = new Date(inputDate);
  101. return date.toLocaleDateString('en-US', options); // 输出格式:May 10, 2025
  102. }