各种时间转换js封装

该博客介绍了JavaScript中用于日期格式转换的实用函数,包括将不同格式的日期字符串转换为标准的'yyyy-MM-dd HH:mm:ss'格式,以及将毫秒数或中国标准时间转化为日期的方法。内容涵盖了日期处理的基本操作,如提取年、月、日、小时、分钟和秒,并提供了示例用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.在utils文件夹引入文件data.js

const date = {
    /**
     * 输入 *
     * "yyyy/MM/dd","yyyy-MM-dd",  "yyyyMMdd","yyyy年MM月dd日"
     * "yyyy年MM月dd日 HH时mm分ss秒", "yyyy-MM-dd HH:mm:ss", "yyyyMMddHHmmss", "yyyy/MM/dd HH:mm:ss"
     *
     * 输出 yyyy-MM-dd HH:mm:ss
     * @param date
     * @param fromType
     * @returns {string}
     */
    formatterDate: function(date, fromType) {
        let DateTime = 'yyyy-MM-dd HH:mm:ss'
        let index = -1
        let lastIndex = ''
        // y
        index = fromType.indexOf('y')
        if (index > -1) {
            lastIndex = index + 4
            DateTime = DateTime.replace('yyyy', date.substring(index, lastIndex))
        } else {
            DateTime = DateTime.replace('yyyy', '2018')
        }
        // M
        index = fromType.indexOf('M')
        if (index > -1) {
            lastIndex = index + 2
            DateTime = DateTime.replace('MM', date.substring(index, lastIndex))
        } else {
            DateTime = DateTime.replace('MM', '01')
        }
        // d
        index = fromType.indexOf('d')
        if (index > -1) {
            lastIndex = index + 2
            DateTime = DateTime.replace('dd', date.substring(index, lastIndex))
        } else {
            DateTime = DateTime.replace('dd', '01')
        }
        // H
        index = fromType.indexOf('H')
        if (index > -1) {
            lastIndex = index + 2
            DateTime = DateTime.replace('HH', date.substring(index, lastIndex))
        } else {
            DateTime = DateTime.replace('HH', '00')
        }
        // m
        index = fromType.indexOf('m')
        if (index > -1) {
            lastIndex = index + 2
            DateTime = DateTime.replace('mm', date.substring(index, lastIndex))
        } else {
            DateTime = DateTime.replace('mm', '00')
        }
        // s
        index = fromType.indexOf('s')
        if (index > -1) {
            lastIndex = index + 2
            DateTime = DateTime.replace('ss', date.substring(index, lastIndex))
        } else {
            DateTime = DateTime.replace('ss', '00')
        }
        return DateTime
    },
      /**
    
       标准日期:2017-09-19 或 2017-09-19 20:00:00
       中国标准时间:Mon Oct 23 2017 17:20:13 GMT+0800 (中国标准时间)
       时间戳:1508750413
       毫秒数:1508750413000
       注意:时间戳*1000就是毫秒数
       时间搓每天间隔: 86400000
       */
    
      /**
       * 毫秒数或中国标准时间转日期
       * @param msec
       * @returns {{hasTime: string, withoutTime: string}}
       */
      msToDate: function(msec) {
        const result = {
          hasTime: '',
          withoutTime: ''
        }
        if (!StringUtils.isNotBlank(msec)) {
          return result
        }
        const datetime = new Date(msec)
        const year = datetime.getFullYear()
        const month = datetime.getMonth()
        const date = datetime.getDate()
        const hour = datetime.getHours()
        const minute = datetime.getMinutes()
        const second = datetime.getSeconds()
    
        /**
         * 日期加时间
         * @type {string}
         */
        const result1 = year +
          '-' +
          ((month + 1) >= 10 ? (month + 1) : '0' + (month + 1)) +
          '-' +
          ((date + 1) <= 10 ? '0' + date : date) +
          ' ' +
          ((hour + 1) <= 10 ? '0' + hour : hour) +
          ':' +
          ((minute + 1) <= 10 ? '0' + minute : minute) +
          ':' +
          ((second + 1) <= 10 ? '0' + second : second)
        /**
         * 日期
         * @type {string}
         */
        const result2 = year +
          '-' +
          ((month + 1) >= 10 ? (month + 1) : '0' + (month + 1)) +
          '-' +
          ((date + 1) <= 10 ? '0' + date : date)
    
        result.hasTime = result1
        result.withoutTime = result2
        return result
      },
       formatDateTime3: function(time, format){  
                var t = new Date(time);  
                var tf = function(i){return (i < 10 ? '0' : '') + i};  
                return format.replace(/yyyy|MM|DD|HH|mm|ss/g, function(a){  
                    switch(a){  
                        case 'yyyy':  
                            return tf(t.getFullYear());  
                            break;  
                        case 'MM':  
                            return tf(t.getMonth() + 1);  
                            break;  
                        case 'mm':  
                            return tf(t.getMinutes());  
                            break;  
                        case 'DD':  
                            return tf(t.getDate());  
                            break;  
                        case 'HH':  
                            return tf(t.getHours());  
                            break;  
                        case 'ss':  
                            return tf(t.getSeconds());  
                            break;  
                    }  
                })  
            }
​
}
export default date
​

2.在页面引入

  import date from '@/utils/date.js'

3.页面使用

标签中:

{{formatterDate(item.updatedTime, 'yyyy-MM-DD')}}

methods:

// 日期转换
            formatterDate(time, FormData) {
                if (time) {
                    return date.formatDateTime3(time, FormData).replace(/-/g, '-')
                }
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值