时间格式转换成指定格式(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"></ ...
随机推荐
- mybatis_01 初运行
maven坐标 <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</ ...
- unity Android路径的相关部分代码
using System; using System.Collections; using System.Collections.Generic; using System.IO; using Uni ...
- 常用IBatis属性
<?xml version="1.0" encoding="utf-8" ?> <sqlMap namespace="GoodDet ...
- h5py学习(一)核心概念
因pandas的to_hdf5函数有bug TypeError: object of type 'int' has no len(),写dataframe数据出现了报错,遂决定直接使用h5py来写数据 ...
- python读取文本中的字典
首先得明确文本的每行是存的json或者用python的write(str(一个字典))写入的,那么不用借助json模块就能读取为字典,使用eval函数就行,json只能处理带双引号的字符串,但很多时候 ...
- git clone 指定分支/指定commit
方法一 下载整个branch及历史记录,文件较大,耗时 git clone --depth 1 [git-url] -b [branch-name] git reset --hard [commit- ...
- Double NaN
import java.math.BigDecimal; import java.math.RoundingMode; public class Test { public static void m ...
- VC 运行时库的引用区别 /MD、/MDd 和 /MT、/MTd和DllMain函数
/MT和/MTd表示采用多线程CRT库的静态lib版本.该选项会在编译时将运行时库以静态lib的形式完全嵌入.该选项生成的可执行文件运行时不需要运行时库dll的参加,会获得轻微的性能提升,但最终生成的 ...
- js转换文件的size由KB转换为B、MB、GB
// 单位转换export function bytesToSize(sizes) { let mYsize = sizes if(mYsize == 0) return 0 + 'B'; if(mY ...
- 尝试在Ubuntu中安装Emacs遇到的问题
出现具体问题 今日学习安装emacs在Ubuntu19.04当中 安装详见下方帖子: 链接: 如何在Ubuntu中安装Emacs. 安装遇到问题,即 sudo add-apt-repository p ...