/**
* Created by PanJiaChen on 16/11/18.
*/
/**
* Parse the time to string
* @param {(Object|string|number)} time
* @param {string} cFormat
* @returns {string | null}
*/
export function parseTime(time, cFormat) {
if (arguments.length === 0 || !time) {
return null
}
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
let date
if (typeof time === 'object') {
date = time
} else {
if ((typeof time === 'string')) {
if ((/^[0-9]+$/.test(time))) {
// support "1548221490638"
time = parseInt(time)
} else {
// support safari
// https://stackoverflow.com/questions/4310953/invalid-date-in-safari
time = time.replace(new RegExp(/-/gm), '/')
}
}
if ((typeof time === 'number') && (time.toString().length === 10)) {
time = time * 1000
}
date = new Date(time)
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
}
const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
const value = formatObj[key]
// Note: getDay() returns 0 on Sunday
if (key === 'a') {
return ['日', '一', '二', '三', '四', '五', '六'][value]
}
return value.toString().padStart(2, '0')
})
return time_str
}
/**
* @param {number} time
* @param {string} option
* @returns {string}
*/
export function formatTime(time, option) {
if (('' + time).length === 10) {
time = parseInt(time) * 1000
} else {
time = +time
}
const d = new Date(time)
const now = Date.now()
const diff = (now - d) / 1000
if (diff < 30) {
return '刚刚'
} else if (diff < 3600) {
// less 1 hour
return Math.ceil(diff / 60) + '分钟前'
} else if (diff < 3600 * 24) {
return Math.ceil(diff / 3600) + '小时前'
} else if (diff < 3600 * 24 * 2) {
return '1天前'
}
if (option) {
return parseTime(time, option)
} else {
return (
d.getMonth() +
1 +
'月' +
d.getDate() +
'日' +
d.getHours() +
'时' +
d.getMinutes() +
'分'
)
}
}
export function copy(str){
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
const selected = document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
el.select();
document.execCommand('copy');
document.body.removeChild(el);
if (selected) {
document.getSelection().removeAllRanges();
document.getSelection().addRange(selected);
}
}
export function formatSeconds(value) {
var secondTime = parseInt(value) // 秒
var minuteTime = 0 // 分
var hourTime = 0 // 小时
var dayTime = 0 // 天
var result = ''
if (value < 60) {
result = secondTime + '秒'
} else {
if (secondTime >= 60) { // 如果秒数大于60,将秒数转换成整数
// 获取分钟,除以60取整数,得到整数分钟
minuteTime = parseInt(secondTime / 60)
// 获取秒数,秒数取佘,得到整数秒数
secondTime = parseInt(secondTime % 60)
// 如果分钟大于60,将分钟转换成小时
if (minuteTime >= 60) {
// 获取小时,获取分钟除以60,得到整数小时
hourTime = parseInt(minuteTime / 60)
// 获取小时后取佘的分,获取分钟除以60取佘的分
minuteTime = parseInt(minuteTime % 60)
if (hourTime >= 24) {
// 获取天数, 获取小时除以24,得到整数天
dayTime = parseInt(hourTime / 24)
// 获取小时后取余小时,获取分钟除以24取余的分;
hourTime = parseInt(hourTime % 24)
}
}
}
if (secondTime > 0) {
secondTime = parseInt(secondTime) >= 10 ? secondTime : '0' + secondTime
result = '' + secondTime + '秒'
}
if (minuteTime > 0) {
minuteTime = parseInt(minuteTime) >= 10 ? minuteTime : '0' + minuteTime
result = '' + minuteTime + '分' + result
}
if (hourTime > 0) {
result = '' + parseInt(hourTime) + '小时' + result
}
if (dayTime > 0) {
result = '' + parseInt(dayTime) + '天' + result
}
}
return result
}
timeTransform.js
最新推荐文章于 2024-11-08 16:09:18 发布
这段代码包含多个时间处理函数,如`parseTime`用于将时间对象或字符串按照指定格式转换为字符串,支持多种输入格式。`formatTime`则根据时间戳生成易读的时间描述,如'刚刚'、'几分钟前'等。此外,还提供了一个`copy`函数用于复制文本到剪贴板。这些函数在前端开发中常用于时间显示和用户交互。
摘要由CSDN通过智能技术生成