You've already forked SmartisanNote.Remake
124 lines
3.5 KiB
JavaScript
124 lines
3.5 KiB
JavaScript
import moment from 'moment'
|
||
|
||
// 设置中文语言包
|
||
import 'moment/locale/zh-cn'
|
||
moment.locale('zh-cn')
|
||
|
||
/**
|
||
* 格式化日期时间
|
||
* @param {string|Date} date - 日期对象或日期字符串
|
||
* @param {string} format - 格式化字符串,默认为 'YYYY-MM-DD HH:mm'
|
||
* @returns {string} 格式化后的日期时间字符串
|
||
*/
|
||
export const formatDateTime = (date, format = 'YYYY-MM-DD HH:mm') => {
|
||
return moment(date).format(format)
|
||
}
|
||
|
||
/**
|
||
* 格式化相对时间(如:5分钟前,2小时前等)
|
||
* @param {string|Date} date - 日期对象或日期字符串
|
||
* @returns {string} 相对时间字符串
|
||
*/
|
||
export const formatRelativeTime = (date) => {
|
||
return moment(date).fromNow()
|
||
}
|
||
|
||
/**
|
||
* 格式化便签列表时间
|
||
* @param {string|Date} date - 日期对象或日期字符串
|
||
* @returns {string} 格式化后的日期时间字符串
|
||
*/
|
||
export const formatNoteListDate = (date) => {
|
||
const momentDate = moment(date)
|
||
const now = moment()
|
||
const diffDays = now.diff(momentDate, 'days')
|
||
|
||
// 今天
|
||
if (diffDays === 0) {
|
||
return momentDate.format('[今天] A h:mm').replace('AM', '上午').replace('PM', '下午')
|
||
}
|
||
|
||
// 昨天
|
||
if (diffDays === 1) {
|
||
return momentDate.format('[昨天] A h:mm').replace('AM', '上午').replace('PM', '下午')
|
||
}
|
||
|
||
// 超过两天但小于一周
|
||
if (diffDays > 1 && diffDays < 7) {
|
||
const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
|
||
return momentDate.format(`[${weekdays[momentDate.day()]}] M/D A h:mm`).replace('AM', '上午').replace('PM', '下午')
|
||
}
|
||
|
||
// 超过一周但小于一年
|
||
if (diffDays >= 7 && diffDays < 365) {
|
||
return momentDate.format(`[${diffDays}天前] M/D A h:mm`).replace('AM', '上午').replace('PM', '下午')
|
||
}
|
||
|
||
// 超过一年
|
||
return momentDate.format(`[${diffDays}天前] YYYY/M/D A h:mm`).replace('AM', '上午').replace('PM', '下午')
|
||
}
|
||
|
||
/**
|
||
* 格式化便签编辑页时间
|
||
* @param {string|Date} date - 日期对象或日期字符串
|
||
* @returns {string} 格式化后的日期时间字符串
|
||
*/
|
||
export const formatNoteEditorDate = (date) => {
|
||
const momentDate = moment(date)
|
||
const now = moment()
|
||
const diffDays = now.diff(momentDate, 'days')
|
||
|
||
// 今天
|
||
if (diffDays === 0) {
|
||
return momentDate.format('[今天] A h:mm').replace('AM', '上午').replace('PM', '下午')
|
||
}
|
||
|
||
// 昨天
|
||
if (diffDays === 1) {
|
||
return momentDate.format('[昨天] A h:mm').replace('AM', '上午').replace('PM', '下午')
|
||
}
|
||
|
||
// 超过两天但小于一个月
|
||
if (diffDays > 1 && diffDays < 30) {
|
||
return momentDate.format('M/D A h:mm').replace('AM', '上午').replace('PM', '下午')
|
||
}
|
||
|
||
// 超过一个月
|
||
return momentDate.format('YYYY/M/D A h:mm').replace('AM', '上午').replace('PM', '下午')
|
||
}
|
||
|
||
/**
|
||
* 获取过去的日期
|
||
* @param {number} amount - 数量
|
||
* @param {string} unit - 单位(days, weeks, months, years等)
|
||
* @returns {string} ISO格式的日期字符串
|
||
*/
|
||
export const getPastDate = (amount, unit) => {
|
||
return moment().subtract(amount, unit).toISOString()
|
||
}
|
||
|
||
/**
|
||
* 获取当前日期时间
|
||
* @returns {string} ISO格式的日期字符串
|
||
*/
|
||
export const getCurrentDateTime = () => {
|
||
return moment().toISOString()
|
||
}
|
||
|
||
/**
|
||
* 获取时间戳
|
||
* @returns {number} 时间戳
|
||
*/
|
||
export const getTimestamp = () => {
|
||
return moment().valueOf()
|
||
}
|
||
|
||
export default {
|
||
formatDateTime,
|
||
formatRelativeTime,
|
||
formatNoteListDate,
|
||
formatNoteEditorDate,
|
||
getPastDate,
|
||
getCurrentDateTime,
|
||
getTimestamp
|
||
} |