date.js

export function formatDate(date, fmt) {
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
}
let o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
};
for (let k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
let str = o[k] + '';
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
}
}
return fmt;
}; function padLeftZero(str) {
return ('00' + str).substr(str.length);
}

组件内使用:


<template>
<div>
<span>{{date | formatDate}}</span>
</div>
</template>
import {formatDate} from './js/date.js';
export default {
date() {
return {
date: 1496311370052
}
},
filters: {
formatDate(time) {
var date = new Date(time);
return formatDate(date, 'yyyy-MM-dd hh : mm');
}
}, }

更新:建议大家直接使用dayjs吧,比我这个好用

vuejs时间格式化的更多相关文章

  1. VUE过滤器的使用 vue 时间格式化

    过滤器介绍 官方教程地址:https://cn.vuejs.org/v2/guide/filters.html 过滤器常被用于一些文本格式化 我们可以自定义过滤器,可以实现各种各样的功能. vue时间 ...

  2. strftime 日期时间格式化

    strftime() 函数根据区域设置格式化本地时间/日期,函数的功能将时间格式化,或者说格式化一个时间字符串. size_t strftime(char *strDest,size_t maxsiz ...

  3. javascript 时间格式化

    添加扩展 //时间格式化扩展Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1 ...

  4. js时间格式化

    const formatDate = timestamp => { const date = new Date(timestamp); const m = date.getMonth() + 1 ...

  5. js对特殊字符转义、时间格式化、获取URL参数

    /*特殊字符转义*/ function replace_html(str) { var str = str.toString().replace(/&/g, "&" ...

  6. 特殊字符转义&时间格式化&获取URL参数

    /*特殊字符转义*/ function htmlspecialchars (str) { var str = str.toString().replace(/&/g, "&& ...

  7. 【AspNetCore】【WebApi】扩展Webapi中的RouteConstraint中,让DateTime类型,支持时间格式化(DateTimeFormat)

    扩展Webapi中的RouteConstraint中,让DateTime类型,支持时间格式化(DateTimeFormat) 一.背景 大家在使用WebApi时,会用到DateTime为参数,类似于这 ...

  8. EasyUI Datagrid Datetime(EasyUI DataGrid 时间格式化)

    EasyUI DataGrid 时间格式化 方法一: var Common = { //EasyUI用DataGrid用日期格式化 TimeFormatter: function (value, re ...

  9. js Date 时间格式化的扩展

    js Date 时间格式化的扩展: Date.prototype.format = function (fmt) { var o = { , //月 "d+": this.getD ...

随机推荐

  1. Error:Error converting bytecode to dex: Cause: com.android.dex.DexException: Multiple dex files define Lcom/lidroid/xutils/task/TaskHandler;

    Error:Error converting bytecode to dex: Cause: com.android.dex.DexException: Multiple dex files defi ...

  2. CSS2.0中最常用的18条技巧

    一.使用css缩写 使用缩写可以帮助减少你CSS文件的大小,更加容易阅读.  具体内容请浏览:CSS常用缩写语法 二.明确定义单位,除非值为0. 忘记定义尺寸的单位是CSS新手普遍的错误.在HTML中 ...

  3. Sword protobuf学习二

    编写protobuf消息文件 文件格式: xxx.proto //标明使用哪个版本的protobuf,默认2.0版本 syntax = "proto3"; //类似于c++中的na ...

  4. 关于Unity中鼠标选取物体的解决方案

    今天修改了之前写的飞机大战的代码,原来的不足之处是点击屏幕的任意一点都可以移动飞机,也就是没有检测鼠标到底有没有点到飞机上. 我先是用之前的3D拾取技术,发现没有反应,才意识到我这个plane飞机节点 ...

  5. C#的publisher与subscriber,事件发布者与订阅者

    说明:示例借鉴自这里,但原版很不友好,于是修改了下,一目了然. 直接上代码: using System; using System.Collections.Generic; using System. ...

  6. 【吉比特】G-bits2018校园春季招聘技术类岗位笔试经验

    笔试公司:厦门吉比特网络技术股份有限公司 笔试岗位:游戏研发工程师 笔试时间:2018年3月30日19:00-20:30 笔试形式:牛客网在线做题 笔试回忆: 笔试总共时长1小时半,共52道题.其中选 ...

  7. 第三百二十九节,web爬虫讲解2—urllib库爬虫—ip代理—用户代理和ip代理结合应用

    第三百二十九节,web爬虫讲解2—urllib库爬虫—ip代理 使用IP代理 ProxyHandler()格式化IP,第一个参数,请求目标可能是http或者https,对应设置build_opener ...

  8. QA:无法为具有固定名称“MySql.Data.MySqlClient”...

    Question: 无法为具有固定名称“MySql.Data.MySqlClient”的 ADO.NET 提供程序加载在应用程序配置文件中注册的实体框架提供程序类型“MySql.Data.MySqlC ...

  9. JS 在火狐浏览器下关闭弹窗

    1.首先,要确定火狐设置是否允许通过JS代码window.close()方法关闭标签. 确定方式如下:      在Firefox地址栏里输入 about:config    在配置列表中找到dom. ...

  10. PHP内置函数生成随机数的方法汇总

    PHP内部生成随机数的方法相比其他方法简单,不需要额外配置,是生成随机数的首选方案. 1 rand函数 rand() 函数可以不加任何参数,就可以生成随机整数.如果要设置随机数范围,可以在函数中设置 ...