时间-时辰 js
警告
本文最后更新于 2024-02-24,文中内容可能已过时。
用于发送的微博/Microblog 的时间记录,1 - 2 kb 的小玩意。
根据时间返回对时间的描述,例如:
- 刚刚
- 22 小时前
- 3 天前 23:45 • 子时 夜半 (半夜)
- 5 天前 01:49 • 丑时 鸡鸣 (凌晨)
- 2000 年 7 月 31 日 00:24 • 子时 夜半 (半夜)
按照时辰-24小时时间-通俗约定,大概范围是:
子时(23-1点):半夜
丑时(1-3点):凌晨
寅时(3-5点):黎明
卯时(5-7点):清晨
辰时(7-9点):早上
巳时(9-11点):上午
午时(11-13点):中午
未时(13-15点):午后
申时(15-17点):下午
酉时(17-19点):傍晚
戌时(19-21点):晚上
亥时(21-23点):深夜
// 十二时辰对应
const tzArr = ['子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥'];
const sdArr = ['夜半', '鸡鸣', '平旦', '日出', '食时', '隅中', '日中', '日昳', '晡时', '日入', '黄昏', '人定'];
const sdArr2 = ['半夜', '凌晨', '黎明', '清晨', '早上', '上午', '中午', '午后', '下午', '傍晚', '晚上', '深夜'];
function formatTime(timestamp) {
const now = new Date();
const inputTime = new Date(timestamp);
// console.log(inputTime)
const timeDiff = now - inputTime;
const seconds = Math.floor(timeDiff / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
if (seconds < 60) {
return "刚刚";
} else if (minutes < 60) {
return `${minutes} 分钟前`;
} else if (hours < 24) {
return `${hours} 小时前`;
} else if (days < 7) {
return `${days} 天前 ${formatHour(inputTime)}`;
}
const yearNow = now.getFullYear();
const yearInput = inputTime.getFullYear();
if (yearNow === yearInput) {
return `${inputTime.getMonth() + 1} 月 ${inputTime.getDate()} 日 ${formatHour(inputTime)}`;
} else {
return `${yearInput} 年 ${inputTime.getMonth() + 1} 月 ${inputTime.getDate()} 日 ${formatHour(inputTime)}`;
}
}
function formatHour(date) {
const hour = date.getHours();
const minute = date.getMinutes();
// 添加判断时辰的逻辑
const tzIndex = Math.floor((hour + 1) / 2) % 12;
const timeString = `${formatNumber(hour)}:${formatNumber(minute)}`;
return `${timeString} • ${tzArr[tzIndex]}时 ${sdArr[tzIndex]} (${sdArr2[tzIndex]})`;
}
function formatNumber(number) {
return number < 10 ? `0${number}` : number;
}
min js
const tzArr=["子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"],sdArr=["夜半","鸡鸣","平旦","日出","食时","隅中","日中","日昳","晡时","日入","黄昏","人定"],sdArr2=["半夜","凌晨","黎明","清晨","早上","上午","中午","午后","下午","傍晚","晚上","深夜"];function formatTime(r){const t=new Date,o=new Date(r),e=t-o,n=Math.floor(e/1e3),u=Math.floor(n/60),f=Math.floor(u/60),a=Math.floor(f/24);if(n<60)return"刚刚";if(u<60)return`${u} 分钟前`;if(f<24)return`${f} 小时前`;if(a<7)return`${a} 天前 ${formatHour(o)}`;const $=t.getFullYear(),m=o.getFullYear();return $===m?`${o.getMonth()+1} 月 ${o.getDate()} 日 ${formatHour(o)}`:`${m} 年 ${o.getMonth()+1} 月 ${o.getDate()} 日 ${formatHour(o)}`}function formatHour(r){const t=r.getHours(),o=r.getMinutes(),e=Math.floor((t+1)/2)%12;return`${`${formatNumber(t)}:${formatNumber(o)}`} • ${tzArr[e]}时 ${sdArr[e]} (${sdArr2[e]})`}function formatNumber(r){return r<10?`0${r}`:r}
测试
// 测试函数
function testFormatTime() {
const testTimestamps = [
Date.now() - 30 * 1000, // 30秒前
Date.now() - 180 * 1000, // 3分钟前
Date.now() - 2 * 3600 * 1000, // 2小时前
Date.now() - 5 * 24 * 3600 * 1000, // 5天前
Date.now() - 10 * 24 * 3600 * 1000, // 10天前
new Date("2023-08-15T08:00:00").getTime(), // 8月15日上午8:00
new Date("2023-03-20T18:30:00").getTime(), // 3月20日下午6:30
new Date("2023-08-19T22:45:00").getTime(), // 当前日期晚上10:45
new Date("2023-01-01T23:00:00").getTime(),
new Date("2023-01-01T00:59:00").getTime(),
new Date("2023-01-01T01:00:00").getTime(),
new Date("2023-01-01T01:59:00").getTime(),
new Date("2023-01-01T00:00:00").getTime(),
new Date("2023-01-01T01:00:00").getTime(),
new Date("2023-01-01T02:00:00").getTime(),
new Date("2023-01-01T03:00:00").getTime(),
new Date("2023-01-01T04:00:00").getTime(),
new Date("2023-01-01T05:00:00").getTime(),
new Date("2023-01-01T06:00:00").getTime(),
new Date("2023-01-01T07:00:00").getTime(),
new Date("2023-01-01T08:00:00").getTime(),
new Date("2023-01-01T09:00:00").getTime(),
new Date("2023-01-01T10:00:00").getTime(),
new Date("2023-01-01T11:00:00").getTime(),
new Date("2023-01-01T12:00:00").getTime(),
new Date("2023-01-01T13:00:00").getTime(),
new Date("2023-01-01T14:00:00").getTime(),
new Date("2023-01-01T15:00:00").getTime(),
new Date("2023-01-01T16:00:00").getTime(),
new Date("2023-01-01T17:00:00").getTime(),
new Date("2023-01-01T18:00:00").getTime(),
new Date("2023-01-01T19:00:00").getTime(),
new Date("2023-01-01T20:00:00").getTime(),
new Date("2023-01-01T21:00:00").getTime(),
new Date("2023-01-01T22:00:00").getTime(),
new Date("2023-01-01T23:00:00").getTime()
];
for (const timestamp of testTimestamps) {
console.log(formatTime(timestamp));
}
}
testFormatTime();
欢迎赞赏~
赞赏