当前位置:网站首页>formatTime时间戳格式转换
formatTime时间戳格式转换
2022-04-23 09:47:00 【观山.】
/**
- 时间戳格式转换以及计算
- */
export function formatTime (time = 0, format = 'YYYY-MM-DD hh:mm:ss') {
const now = new Date().getTime()
if (!time) time = now
while (time.toString().length < 13) time += '0'
const date = new Date(time)
date.getMonth()
/** 参数集 年-月-日 时:分:秒 */
const arg = {
year: date.getFullYear(),
month: date.getMonth() + 1,
day: date.getDate(),
hours: date.getHours(),
minutes: date.getMinutes(),
seconds: date.getSeconds()
}
/** 判断有没有指定的时间格式 */
switch (format) {
case 'YYYY-MM-DD hh:mm:ss':
return `${
arg.year}-${
arg.month}-${
arg.day} ${
arg.hours}:${
arg.minutes}:${
arg.seconds}`
case 'YYYY-MM-DD':
return `${
arg.year}-${
arg.month}-${
arg.day}`
case 'MM-DD':
return `${
arg.month}-${
arg.day}`
case 'hh:mm:ss':
return `${
arg.hours}:${
arg.minutes}:${
arg.seconds}`
case 'hh:mm':
return `${
arg.hours}:${
arg.minutes}`
case 'computed': //判断是不是需要进行计算
if (now > time) {
const dt = Math.abs(time - now), //时间已过去多少毫秒
S = dt / 1000, //秒
M = S / 60, //分
H = M / 60, //小时
D = H / 24, //天
W = D / 7 //周
/**
~~ ==>表示取整数部分 类似与 parseInt
*/
if (~~W > 0 && W < 3) {
return ~~W + '周前'
} else if (D < 7 && ~~D > 0) {
return ~~D + '天前'
} else if (~~H > 0 && H < 24) {
return ~~H + '小时前'
} else if (~~M > 0 && M < 60) {
return ~~M + '分钟前'
} else if (~~S > 0 && S < 60) {
return ~~S + '秒前'
}
} else {
console.log('未来的时间')
}
return `${
arg.year}-${
arg.month}-${
arg.day} ${
arg.hours}:${
arg.minutes}:${
arg.seconds}`
}
}
版权声明
本文为[观山.]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_47733727/article/details/124287323
边栏推荐
- 防疫登记小程序
- JS what is an event? Event three elements and operation elements
- Cloud computing competition -- basic part of 2020 competition [task 3]
- Chinese Remainder Theorem and extended Chinese remainder theorem that can be understood by Aunt Baojie
- SAP CR transmission request sequence and dependency check
- MySQL of database -- Fundamentals
- Leetcode0587. 安装栅栏(difficult)
- GUI, CLI and UNIX Philosophy
- 工业元宇宙平台规划与建设
- JS scope, scope chain, global variables and local variables
猜你喜欢
Leetcode0587. Install fence
Go language learning notes - structure | go language from scratch
kernel-pwn学习(4)--Double Fetch&&0CTF2018-baby
高薪程序员&面试题精讲系列91之Limit 20000加载很慢怎么解决?如何定位慢SQL?
SAP salv14 background output salv data can directly save files and send emails (with sorting, hyperlink and filtering format)
Alibaba cloud architects interpret the four mainstream game architectures
Exclusive thoughts and cases of JS
Number of islands
Amazon cloud technology entry Resource Center, easy access to the cloud from 0 to 1
SAP excel has completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.
随机推荐
JS DOM event
JS and how to judge custom attributes in H5
Redis 内存占满导致的 Setnx 命令执行失败
[ACM-ICPC 2018 Shenyang Network preliminaries] J. Ka Chang (block + DFS sequence)
Es aggregation aggregation analysis
Nine abilities of agile manufacturing in the era of meta universe
Planning and construction of industrial meta universe platform
Introduction to graph theory -- drawing
SAP ECC connecting SAP pi system configuration
NLLLoss+log_ SoftMax=CE_ Loss
[hdu6868] absolute math (pusher + Mobius inversion)
Kettle experiment (III)
Redis 过期 key 清理删除策略汇总
Vivo, hardware safe love and thunder
How to use SQL statement union to get another column of another table when the content of a column in a table is empty
Mobius inversion
《信息系统项目管理师总结》第八章 项目干系人管理
[reading notes] Chapter 5 conditional statements, circular statements and block statements of Verilog digital system design tutorial (with answers to thinking questions)
Career planning and implementation in the era of meta universe
The sap export excel file opens and shows that the file format and extension of "XXX" do not match. The file may be damaged or unsafe. Do not open it unless you trust its source. Do you still want to