feat(时间格式化): 添加自定义时间格式化函数并替换dayjs使用
添加timeFormat函数用于更灵活地处理时间格式化,支持多种格式和不同时间戳类型 移除dayjs依赖,使用原生Date对象实现时间格式化功能
This commit is contained in:
parent
df0b47d766
commit
a775247a9f
|
|
@ -43,7 +43,11 @@
|
||||||
<view class="news-meta">
|
<view class="news-meta">
|
||||||
<view>
|
<view>
|
||||||
<text class="source">{{ item.source }}</text>
|
<text class="source">{{ item.source }}</text>
|
||||||
<text class="time">{{ dayjs(item.publish_time).format("YYYY-MM-DD HH:MM:ss") }}</text>
|
<!-- <text class="time">{{ dayjs(item.publish_time).format("YYYY-MM-DD HH:MM:ss") }}</text> -->
|
||||||
|
<!-- .format('YYYY-MM-DD HH:mm:ss'); -->
|
||||||
|
<text class="time">
|
||||||
|
{{ timeFormat(new Date(item.publish_time).getTime()) }}
|
||||||
|
</text>
|
||||||
</view>
|
</view>
|
||||||
<text class="score" v-if="needExp">
|
<text class="score" v-if="needExp">
|
||||||
<text v-if="index < 3">资讯评分:</text>
|
<text v-if="index < 3">资讯评分:</text>
|
||||||
|
|
@ -70,7 +74,7 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, watch } from "vue";
|
import { ref, watch, computed } from "vue";
|
||||||
import dayjs from "dayjs/esm/index";
|
import dayjs from "dayjs/esm/index";
|
||||||
import LoginPopup from "@/components/loginPopup/index.vue";
|
import LoginPopup from "@/components/loginPopup/index.vue";
|
||||||
import { Session } from "@/utils/storage";
|
import { Session } from "@/utils/storage";
|
||||||
|
|
@ -170,6 +174,54 @@ const handlePopupSuccessCallback = () => {
|
||||||
const handlePopupErrorCallback = () => {
|
const handlePopupErrorCallback = () => {
|
||||||
console.log("登录失败");
|
console.log("登录失败");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description 格式化时间
|
||||||
|
* @param {String|Number} dateTime 需要格式化的时间戳
|
||||||
|
* @param {String} fmt 格式化规则 yyyy:mm:dd|yyyy:mm|yyyy年mm月dd日|yyyy年mm月dd日 hh时MM分等,可自定义组合 默认yyyy-mm-dd
|
||||||
|
* @returns {string} 返回格式化后的字符串
|
||||||
|
*/
|
||||||
|
function timeFormat(dateTime = null, formatStr = "yyyy-mm-dd hh:MM:ss") {
|
||||||
|
let date;
|
||||||
|
// 若传入时间为假值,则取当前时间
|
||||||
|
if (!dateTime) {
|
||||||
|
date = new Date();
|
||||||
|
}
|
||||||
|
// 若为unix秒时间戳,则转为毫秒时间戳(逻辑有点奇怪,但不敢改,以保证历史兼容)
|
||||||
|
else if (/^\d{10}$/.test(dateTime.toString().trim())) {
|
||||||
|
date = new Date(dateTime * 1000);
|
||||||
|
}
|
||||||
|
// 若用户传入字符串格式时间戳,new Date无法解析,需做兼容
|
||||||
|
else if (typeof dateTime === "string" && /^\d+$/.test(dateTime.trim())) {
|
||||||
|
date = new Date(Number(dateTime));
|
||||||
|
}
|
||||||
|
// 其他都认为符合 RFC 2822 规范
|
||||||
|
else {
|
||||||
|
// 处理平台性差异,在Safari/Webkit中,new Date仅支持/作为分割符的字符串时间
|
||||||
|
date = new Date(typeof dateTime === "string" ? dateTime.replace(/-/g, "/") : dateTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
const timeSource = {
|
||||||
|
y: date.getFullYear().toString(), // 年
|
||||||
|
m: (date.getMonth() + 1).toString().padStart(2, "0"), // 月
|
||||||
|
d: date.getDate().toString().padStart(2, "0"), // 日
|
||||||
|
h: date.getHours().toString().padStart(2, "0"), // 时
|
||||||
|
M: date.getMinutes().toString().padStart(2, "0"), // 分
|
||||||
|
s: date.getSeconds().toString().padStart(2, "0"), // 秒
|
||||||
|
// 有其他格式化字符需求可以继续添加,必须转化成字符串
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const key in timeSource) {
|
||||||
|
const [ret] = new RegExp(`${key}+`).exec(formatStr) || [];
|
||||||
|
if (ret) {
|
||||||
|
// 年可能只需展示两位
|
||||||
|
const beginIndex = key === "y" && ret.length === 2 ? 2 : 0;
|
||||||
|
formatStr = formatStr.replace(ret, timeSource[key].slice(beginIndex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return formatStr;
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped lang="scss">
|
<style scoped lang="scss">
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue