时间格式转换成指定格式(Vue)
1 /**
2 * Parse the time to string
3 * @param {(Object|string|number)} time
4 * @param {string} cFormat
5 * @returns {string | null}
6 */
7 export function parseTime(time, cFormat) {
8 if (arguments.length === 0 || !time) {
9 return null
10 }
11 const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
12 let date
13 if (typeof time === 'object') {
14 date = time
15 } else {
16 if ((typeof time === 'string')) {
17 if ((/^[0-9]+$/.test(time))) {
18 // support "1548221490638"
19 time = parseInt(time)
20 } else {
21 // support safari
22 // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
23 time = time.replace(new RegExp(/-/gm), '/')
24 }
25 }
26
27 if ((typeof time === 'number') && (time.toString().length === 10)) {
28 time = time * 1000
29 }
30 date = new Date(time)
31 }
32 const formatObj = {
33 y: date.getFullYear(),
34 m: date.getMonth() + 1,
35 d: date.getDate(),
36 h: date.getHours(),
37 i: date.getMinutes(),
38 s: date.getSeconds(),
39 a: date.getDay()
40 }
41 const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
42 const value = formatObj[key]
43 // Note: getDay() returns 0 on Sunday
44 if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
45 return value.toString().padStart(2, '0')
46 })
47 return time_str
48 }
49
50 /**
51 * @param {number} time
52 * @param {string} option
53 * @returns {string}
54 */
55 export function formatTime(time, option) {
56 if (('' + time).length === 10) {
57 time = parseInt(time) * 1000
58 } else {
59 time = +time
60 }
61 const d = new Date(time)
62 const now = Date.now()
63
64 const diff = (now - d) / 1000
65
66 if (diff < 30) {
67 return '刚刚'
68 } else if (diff < 3600) {
69 // less 1 hour
70 return Math.ceil(diff / 60) + '分钟前'
71 } else if (diff < 3600 * 24) {
72 return Math.ceil(diff / 3600) + '小时前'
73 } else if (diff < 3600 * 24 * 2) {
74 return '1天前'
75 }
76 if (option) {
77 return parseTime(time, option)
78 } else {
79 return (
80 d.getMonth() +
81 1 +
82 '月' +
83 d.getDate() +
84 '日' +
85 d.getHours() +
86 '时' +
87 d.getMinutes() +
88 '分'
89 )
90 }
91 }
时间格式转换成指定格式(Vue)的更多相关文章
- javascript时间戳转换成指定格式的日期
//时间戳转换成指定格式的日期DateTool.IntDatetimeTo = function(time, format){ var testDate = new Date(time); ...
- linux环境下deb格式 转换成rpm格式
linux环境下deb格式 转换成rpm格式 使用alien工具转换deb格式到rpm格式 alien_8.87.tar.gz 下载alien_8.87.tar.gz [root@mysqlnode2 ...
- 怎样将M4A音频格式转换成MP3格式
因为MP3音频格式应用的广泛性,所以很多时候我们都需要将不同的音频格式转换成MP3格式的,那么如果我们需要将M4A音频格式转换成MP3格式,我们应该怎样进行实现呢?下面我们就一起来看一下吧. 操作步骤 ...
- 分别用Excel和python进行日期格式转换成时间戳格式
最近在处理一份驾驶行为方面的数据,其中要用到时间戳,因此就在此与大家一同分享学习一下. 1.什么是时间戳? 时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01 ...
- 怎样将flac音频格式转换成MP3格式
Flac音频格式怎样转换成MP3格式呢?随着现在音频格式的不断多样性,生活中很多时候我们都会遇到音频格式转换的问题,如flac音频转MP3的问题,那么我们应该如何去解决这个问题呢?下面我们就一起去来一 ...
- windwos文档格式转换成unix格式
在工作学习中我们避免不了需要将一些脚本和命令记录在笔记里面,我使用的是有道云笔记,每当我将上次记录在有道云的脚本复制出来进行使用的时候,总会报一些奇怪的错误,要么是包含换行符,要么就是格式不对,但是我 ...
- 将ERF格式转换成PCAP格式
在研究网络流量分析的时候,wireshark默认采用pcap格式.对于用Endace DAG捕捉卡捕获的数据包,一般来说,都是erf格式的.一般来说,此种格式包含了更多了链路层信息.而我们采用wire ...
- 把#define宏转换成指定格式
之前在弄一个东西的,有一大堆的宏,需要把它转换成其它的形式.遇到这种大批量的东西,我特别没有耐心去一个一个的弄,于是写了一段代码. 估计大家平常比较难用得上,不过可以平常相似的情况用来参考. Sort ...
- dos2unix 将DOS格式转换成NUIX格式
1.命令功能 dos2unix将windows文件格式转换成unix文件格式. 2.语法格式 dos2unix file 3.使用范例 [root@localhost ~]# dos2unix wi ...
- jquery将日期转换成指定格式的字符串
引用jquery文件,如<script type="text/javascript" src="jquery-1.8.3.min.js"></ ...
随机推荐
- Vue 事件监听
事件监听 v-on 使用v-on进行事件绑定监听,回调函数写在methods中.可以使用@的这种简写形式来代替v-on,当事件源无参数传递时,可省略括号. 语法如下所示: <button @:事 ...
- sqoop,hive2mysql
sqoop export \ --connect jdbc:mysql://master:3306/testdb \ --username hive \ --password 123456 \ --t ...
- 从FGUI中取一张图片并返回一个Sprite
从Fgui中的图集中取一个图素,把图素用到场景等非UI的地方. 此操作会动态创建一个Sprite对象,效率不好,不适合大量使用. private static Dictionary<string ...
- vue v-if不生效
正确写法 <block v-for="(item, index) in imgArray"> <image :src="item" class ...
- CSC落榜
2021年5月31日21:00点,CSC公布结果,未通过.看到这,我感觉空气瞬间凝固,窒息,那一瞬间我无比平静,我以为我会哭,但是,却泣不成声,脑中第一时间想到得是,如何面对认识得人,全世界感觉都知道 ...
- JDBC之Driver和DriverMananger
JDBC之Driver和DriverMananger 目录 JDBC之Driver和DriverMananger Java和MySQL的关系 JDBC 演变过程 驱动加载入内存的过程 Oracle加载 ...
- 【11】python之循环
Python 中的循环语句有 for 和 while. 1.while 循环 Python 中 while 语句的一般形式: while 判断条件(condition): 执行语句(statement ...
- Word08 创新产品展示说明会邀请函office真题
1.课程的讲解之前,先来对题目进行分析,首先需要在考生文件夹下,将Wrod素材.docx文件另存为Word.docx,后续操作均基于此文件,否则不得分. 2.这一步非常的简单,打开下载素材文件,在[文 ...
- c++ 继承访问控制初步
访问控制方式这里有篇很好的文章,其实内容也是总结c++primer上的内容 现在就按照这篇的文章举例进行学习. 思路 不同继承方式的影响主要体现在: 1.派生类成员对基类成员的访问控制. 2.派生类对 ...
- wxPython绘图API
简单介绍一个Pthon的绘图库wxPython. GDI+(图形绘制接口),CoreGraphics和Cairo库形成wxPython绘图API的框架.wx.GraphicsContext是主要绘制对 ...