123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- // 下载
- export function downloadFile (url) {
- if (typeof window !== "undefined") {
- const userAgent = navigator.userAgent;
- if (userAgent.indexOf("Android") > -1) {
- window.open(url);
- } else if (
- userAgent.indexOf("iPhone") > -1 ||
- userAgent.indexOf("iPad") > -1 ||
- userAgent.indexOf("iPod") > -1
- ) {
- location.href = url;
- } else {
- window.open(url);
- }
- }
- }
- // js日期格式化(xxxx年xx月xx日)
- export function dateFomat (dateStr: String) {
- var result = "";
- if (dateStr != "") {
- var contractdate = new Date(dateStr);
- var contract_year = contractdate.getFullYear();
- var contract_month = (contractdate.getMonth() + 1) < 10 ? '0' + (contractdate.getMonth() + 1) : (contractdate.getMonth() + 1);
- var contract_day = contractdate.getDate() < 10 ? '0' + contractdate.getDate() : contractdate.getDate();
- result = contract_year + '年' + contract_month + '月' + contract_day + '日';
- }
- return result;
- }
- // 获取标签中内容
- export function getStringContent (html: String, tagName: String) {
- const regex = new RegExp(`<${tagName}.*?>(.*?)</${tagName}>`, 's'); // 's' 允许 . 匹配换行符
- const match = html.match(regex);
- return match ? match[1] : null;
- }
- export function getCookieLang () {
- let lang = useCookie('i18n_redirected').value;
- return lang
- }
- /**
- * 日期时间格式化未字符串输出
- * @param {(Date | string)} date
- * @param {string} format
- */
- export function formatDate (date: Date | string | number, format: string) {
- if (!date) {
- return "";
- }
- try {
- const value: Date = new Date(date);
- const yyyy = value.getFullYear();
- const M = value.getMonth() + 1;
- const MM =
- value.getMonth() + 1 >= 10
- ? value.getMonth() + 1
- : "0" + (value.getMonth() + 1);
- const d = value.getDate();
- const dd = value.getDate() >= 10 ? value.getDate() : "0" + value.getDate();
- const H = value.getHours();
- const HH =
- value.getHours() >= 10 ? value.getHours() : "0" + value.getHours();
- const m = value.getMinutes();
- const mm =
- value.getMinutes() >= 10 ? value.getMinutes() : "0" + value.getMinutes();
- const s = value.getSeconds();
- const ss =
- value.getSeconds() >= 10 ? value.getSeconds() : "0" + value.getSeconds();
- if (format === "yyyy") {
- return yyyy;
- } else if (format === "yyyy-MM") {
- return yyyy + "-" + MM;
- } else if (format === "yyyy-M") {
- return yyyy + "-" + M;
- } else if (format === "yyyy-MM-dd") {
- return yyyy + "-" + MM + "-" + dd;
- } else if (format === "yyyy-M-d") {
- return yyyy + "-" + M + "-" + d;
- } else if (format === "HH:mm:ss") {
- return HH + ":" + mm + ":" + ss;
- } else if (format === "H:m:s") {
- return H + ":" + m + ":" + s;
- } else if (format === "HH:mm") {
- return HH + ":" + mm;
- } else if (format === "H:m") {
- return H + ":" + m;
- } else if (format === "yyyy-MM-dd HH:mm") {
- return yyyy + "-" + MM + "-" + dd + " " + HH + ":" + mm;
- } else if (format === "yyyy-MM-dd HH:mm:ss") {
- return yyyy + "-" + MM + "-" + dd + " " + HH + ":" + mm + ":" + ss;
- } else {
- return yyyy + "-" + MM + "-" + dd;
- }
- } catch (error) {
- return "";
- }
- }
- // js日期格式化(May 10, 2025)
- export function formatDateEn (inputDate: any) {
- let options = { year: 'numeric', month: 'long', day: 'numeric' };
- let date = new Date(inputDate);
- return date.toLocaleDateString('en-US', options); // 输出格式:May 10, 2025
- }
|