微信小程序实现日期格式化

1、获取时间戳格式的系统日期

 //获取系统时间
        var nowtime = new Date().getTime()
        console.log('现在的时间是'+nowtime)

输出:

现在的时间是1615360040098

2、将时间戳格式的日期转换成yyyy-MM-dd HH:MM:SS

 /**
     * 
     * @param {*} times  时间戳
     * 转换为  yyyy-MM-dd HH:MM:SS 格式的日期
     */
    formatDate: function (times) {
        var date = new Date(times);
        var year = date.getFullYear(); //年份
        var month = date.getMonth() + 1; //月份
        var day = date.getDate(); //日
        var hour = function () { //获取小时
            return date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
        }
        var minute = function () { //获取分钟
            return date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
        }

        var second = function () { //获取秒数
            return date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
        }
        return year + '-' + month + '-' + day + ' ' + hour() + ':' + minute() + ':' + second()

    },

调用和输出:

 var that = this;
        //    获取系统时间
        var nowtime = new Date().getTime()
        console.log(that.formatDate(nowtime))

在这里插入图片描述
3、将yyyy-MM-dd HH:MM:SS格式的日期转化为时间戳

 /**
     * 
     * @param {*} times yyyy-MM-dd HH:MM:SS 格式的日期
     * 转换成时间戳
     */
    toDate: function (times) {
        var date = new Date(times).getTime();
        console.log('date是:'+date)
        return date
    },

输出:
在这里插入图片描述

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值