当前位置:网站首页>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
边栏推荐
- F-niu Mei's apple tree (diameter combined)
- [hdu6868] absolute math (pusher + Mobius inversion)
- P1446 [hnoi2008] cards (Burnside theorem + DP count)
- Go language learning notes - language interface | go language from scratch
- Educational Codeforces Round 81 (Rated for Div. 2)
- [ACM-ICPC 2018 Shenyang Network preliminaries] J. Ka Chang (block + DFS sequence)
- Leetcode题库78. 子集(递归 c实现)
- Compile and debug mysql8 with clion under MacOS x
- Redis 内存占满导致的 Setnx 命令执行失败
- Prefix sum of integral function -- Du Jiao sieve
猜你喜欢

Setnx command execution failed due to full redis memory

ABAP implementation publishes restful services for external invocation example

云身份过于宽松,为攻击者打开了大门

kernel-pwn学习(3)--ret2user&&kernel ROP&&QWB2018-core

Number theory blocking (integer division blocking)

Yyds dry goods inventory ubuntu18 0.4 install MySQL and solve error 1698: access denied for user ''root' '@' 'localhost' '

SAP pi / PO soap2proxy consumption external WS example

Go language learning notes - slice, map | go language from scratch

Acquisition of DOM learning elements JS

MySQL of database -- Fundamentals
随机推荐
golang力扣leetcode 396.旋转函数
1D / 1D dynamic programming learning summary
Give the method of instantiating the object to the new object
Random neurons and random depth of dropout Technology
PHP notes (I): development environment configuration
如何实现根据照片获取地理位置及如何防御照片泄漏地理位置
Redis 过期 key 清理删除策略汇总
Redis 内存占满导致的 Setnx 命令执行失败
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》
[hdu6833] a very easy math problem
SAP debug debug for in, reduce and other complex statements
JSON input of Chapter 14 of kettle paoding jieniu
Easy to understand subset DP
Prefix sum of integral function -- Du Jiao sieve
Kettle experiment (III)
阿里云架构师解读四大主流游戏架构
理解作用域
php 二维数组指定元素相等后相加否则新增
Canary publishing using ingress
SAP 03-amdp CDs table function using 'with' clause