JS格式化时间(支持小程序,兼容IOS)

1const REGEX = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/ 2/** 3 * @function format time 4 * @param val, format 5 * @return {string} 6 * @example 7 * <template> 8 * <div> 9 * <span>{{item.time | formatTime('yyyy/MM/dd hh:mm:ss')}}</span> 10 * </div> 11 * </template> 12 * import {formatTime} from '../../library/timeFormat' 13 * export default { 14 * filters: {formatTime} 15 * } 16 */ 17export const formatTime = (val, format) => { 18 if (val) { 19 /** 20 * @instructions 如果不是时间戳格式,且含有字符 '-' 则将 '-' 替换成 '/' && 删除小数点及后面的数字 21 * @reason 将 '-' 替换成 '/' && 删除小数点及后面的数字 的原因是safari浏览器仅支持 '/' 隔开的时间格式 22 */ 23 if (val.toString().indexOf('-') > 0) { 24 val = val.replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '').replace(/(-)/g, '/') // 将 '-' 替换成 '/' 25 val = val.slice(0, val.indexOf('.')) // 删除小数点及后面的数字 26 } 27 let date = new Date(val) 28 date.setHours(date.getHours() + 8) 29 const [whole, yy, MM, dd, hh, mm, ss] = date.toISOString().match(REGEX) 30 const year = new Date().getFullYear() 31 const month = new Date().getMonth() + 1 32 const dates = new Date().getDate() 33 if (format) { 34 return format 35 .replace('yyyy', yy) 36 .replace('yy', yy.slice(2)) 37 .replace('MM', MM) 38 .replace('dd', dd) 39 .replace('hh', hh) 40 .replace('mm', mm) 41 .replace('ss', ss) 42 } else { 43 return [yy, MM, dd].join('-') + ' ' + [hh, mm, ss].join(':') 44 } 45 } else { 46 return '--' 47 } 48}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

微信小程序new Date()转换时间异常问题

微信小程序苹果手机页面上显示时间异常,安卓机正常问题image(https://imghelloworld.osscnbeijing.aliyuncs.com/imgs/b691e1230e2f15efbd81fe11ef734d4f.png)错误代码vardate'2021030617:00:00'vardateT

AndroidStudio封装SDK的那些事

<divclass"markdown\_views"<!flowchart箭头图标勿删<svgxmlns"http://www.w3.org/2000/svg"style"display:none;"<pathstrokelinecap"round"d"M5,00,2.55,5z"id"raphael

KVM调整cpu和内存

一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid