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

Wesley13
• 阅读 471
const REGEX = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/
/**
 * @function format time
 * @param val, format
 * @return {string}
 * @example
 *      <template>
 *          <div>
 *             <span>{{item.time | formatTime('yyyy/MM/dd hh:mm:ss')}}</span>
 *          </div>
 *       </template>
 *       import {formatTime} from '../../library/timeFormat'
 *       export default {
 *          filters: {formatTime}
 *       }
 */
export const formatTime = (val, format) => {
    if (val) {
        /**
         * @instructions 如果不是时间戳格式,且含有字符 '-' 则将 '-' 替换成 '/' && 删除小数点及后面的数字
         * @reason 将 '-' 替换成 '/' && 删除小数点及后面的数字 的原因是safari浏览器仅支持 '/' 隔开的时间格式
         */
        if (val.toString().indexOf('-') > 0) {
            val = val.replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '').replace(/(-)/g, '/') // 将 '-' 替换成 '/'
            val = val.slice(0, val.indexOf('.')) // 删除小数点及后面的数字
        }
        let date = new Date(val)
        date.setHours(date.getHours() + 8)
        const [whole, yy, MM, dd, hh, mm, ss] = date.toISOString().match(REGEX)
        const year = new Date().getFullYear()
        const month = new Date().getMonth() + 1
        const dates = new Date().getDate()
        if (format) {
            return format
                .replace('yyyy', yy)
                .replace('yy', yy.slice(2))
                .replace('MM', MM)
                .replace('dd', dd)
                .replace('hh', hh)
                .replace('mm', mm)
                .replace('ss', ss)
        } else {
            return [yy, MM, dd].join('-') + ' ' + [hh, mm, ss].join(':')
        }
    } else {
        return '--'
    }
}
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
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
梦
3年前
微信小程序new Date()转换时间异常问题
微信小程序苹果手机页面上显示时间异常,安卓机正常问题image(https://imghelloworld.osscnbeijing.aliyuncs.com/imgs/b691e1230e2f15efbd81fe11ef734d4f.png)错误代码vardate'2021030617:00:00'vardateT
Easter79 Easter79
2年前
springboot的起步依赖
!(https://oscimg.oschina.net/oscnet/f3acbe4cf3b00c68207e091c172d6b45a27.png)加载自动配置的方式2:!(https://oscimg.oschina.net/oscnet/40341228c10f7a56d82323a1d622521d92d.png) spring
Stella981 Stella981
2年前
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
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Stella981 Stella981
2年前
Spring Boot日志集成
!(https://oscimg.oschina.net/oscnet/1bde8e8d00e848be8b84e9d1d44c9e5c.jpg)SpringBoot日志框架SpringBoot支持JavaUtilLogging,Log4j2,Lockback作为日志框架,如果你使用star
Wesley13 Wesley13
2年前
17张程序员专用的电脑壁纸
!(https://oscimg.oschina.net/oscnet/220dde2448be4f7090da175592bd00f9.gif)1、三思后再写代码!!!!(https://oscimg.oschina.net/oscnet/2babd54776e4453d9872ae80efb1fa1d.png)
Stella981 Stella981
2年前
Python time模块 返回格式化时间
常用命令  strftimetime.strftime("%Y%m%d%H:%M:%S",formattime)第二个参数为可选参数,不填第二个参数则返回格式化后的当前时间日期201812112:00:00time.strftime('%H:%M:%S')返回当前时间的时分秒time.strftim
Stella981 Stella981
2年前
Dubbo爆出严重漏洞!可导致网站被控制、数据泄露!附解决方案
http://dy.163.com/v2/article/detail/F5FPIFRU0511Q1AF.html  !(http://dingyue.ws.126.net/2020/0216/125ec4c4p00q5rcrs0019d200ig009qg00ig009q.png)  来源:华为云  原文地址:https://w
鲍忠 鲍忠
1年前
2022升级百度大牛带你结合实践重学C++上山的阿科姿
!16634324571b7f1262efa6593(https://imghelloworld.osscnbeijing.aliyuncs.com/imgs/d7d539220bfe32bfc2aa6c00b6abeca4.png)