Merge pull request #102 from dh336699/feature-issue-94

This commit is contained in:
jxxghp
2024-04-17 12:44:17 +08:00
committed by GitHub
2 changed files with 28 additions and 20 deletions
+2 -1
View File
@@ -81,6 +81,7 @@
"@vitejs/plugin-vue": "^5.0.4", "@vitejs/plugin-vue": "^5.0.4",
"@vitejs/plugin-vue-jsx": "^3.0.0", "@vitejs/plugin-vue-jsx": "^3.0.0",
"autoprefixer": "^10.4.14", "autoprefixer": "^10.4.14",
"dayjs": "^1.11.10",
"eslint": "^9.0.0", "eslint": "^9.0.0",
"eslint-config-airbnb-base": "^15.0.0", "eslint-config-airbnb-base": "^15.0.0",
"eslint-import-resolver-typescript": "^3.5.1", "eslint-import-resolver-typescript": "^3.5.1",
@@ -112,4 +113,4 @@
"resolutions": { "resolutions": {
"postcss": "8" "postcss": "8"
} }
} }
+26 -19
View File
@@ -1,5 +1,12 @@
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
import ZH_CN from 'dayjs/locale/zh-cn'
import { isToday } from './index' import { isToday } from './index'
dayjs.extend(relativeTime)
dayjs.locale(ZH_CN)
export function avatarText(value: string) { export function avatarText(value: string) {
if (!value) if (!value)
return '' return ''
@@ -19,7 +26,7 @@ export function kFormatter(num: number) {
* Format and return date in Humanize format * Format and return date in Humanize format
* Intl docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/format * Intl docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/format
* Intl Constructor: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat * Intl Constructor: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat
* @param {String} value date to format * @param {string} value date to format
* @param {Intl.DateTimeFormatOptions} formatting Intl object to format with * @param {Intl.DateTimeFormatOptions} formatting Intl object to format with
*/ */
export function formatDate(value: string, formatting: Intl.DateTimeFormatOptions = { month: 'short', day: 'numeric', year: 'numeric' }) { export function formatDate(value: string, formatting: Intl.DateTimeFormatOptions = { month: 'short', day: 'numeric', year: 'numeric' }) {
@@ -32,8 +39,8 @@ export function formatDate(value: string, formatting: Intl.DateTimeFormatOptions
/** /**
* Return short human friendly month representation of date * Return short human friendly month representation of date
* Can also convert date to only time if date is of today (Better UX) * Can also convert date to only time if date is of today (Better UX)
* @param {String} value date to format * @param {string} value date to format
* @param {Boolean} toTimeForCurrentDay Shall convert to time if day is today/current * @param {boolean} toTimeForCurrentDay Shall convert to time if day is today/current
*/ */
export function formatDateToMonthShort(value: string, toTimeForCurrentDay = true) { export function formatDateToMonthShort(value: string, toTimeForCurrentDay = true) {
const date = new Date(value) const date = new Date(value)
@@ -107,7 +114,7 @@ export function formatBytes(bytes: number, decimals = 2) {
const i = Math.floor(Math.log(bytes) / Math.log(k)) const i = Math.floor(Math.log(bytes) / Math.log(k))
return `${parseFloat((bytes / k ** i).toFixed(dm))} ${sizes[i]}` return `${Number.parseFloat((bytes / k ** i).toFixed(dm))} ${sizes[i]}`
} }
// 格式化剧集列表 // 格式化剧集列表
@@ -150,20 +157,20 @@ export function formatEp(nums: number[]): string {
// 将yyyy-mm-dd hh:mm:ss转换为时间差,如:1小时前,1天前 // 将yyyy-mm-dd hh:mm:ss转换为时间差,如:1小时前,1天前
export function formatDateDifference(dateString: string): string { export function formatDateDifference(dateString: string): string {
const date = new Date(dateString) // const timeDifference = dayjs().millisecond() - dayjs(dateString).millisecond()
const currentDate = new Date() // const secondsDifference = Math.floor(timeDifference / 1000)
const timeDifference = currentDate.getTime() - date.getTime() // const minutesDifference = Math.floor(secondsDifference / 60)
const secondsDifference = Math.floor(timeDifference / 1000) // const hoursDifference = Math.floor(minutesDifference / 60)
const minutesDifference = Math.floor(secondsDifference / 60) // const daysDifference = Math.floor(hoursDifference / 24)
const hoursDifference = Math.floor(minutesDifference / 60)
const daysDifference = Math.floor(hoursDifference / 24)
if (daysDifference > 0) // if (daysDifference > 0)
return `${daysDifference}天前` // return `${daysDifference}天前`
else if (hoursDifference > 0) // else if (hoursDifference > 0)
return `${hoursDifference}小时前` // return `${hoursDifference}小时前`
else if (minutesDifference > 0) // else if (minutesDifference > 0)
return `${minutesDifference}分钟前` // return `${minutesDifference}分钟前`
else // else
return '刚刚' // return '刚刚'
return dayjs(dateString).fromNow()
} }