时间戳与时间的转换

时间戳转时间格式(年:月:日:天:时:分)
formatDate:function(nows) {
        var date = new Date(nows);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        return y + '-' + m + '-' + d + ' ' + h + ':' + minute;
    },
时间格式转时间戳
var data = new Data();     //获取当前时间
var gettime = date.getTime()             //转为时间戳

计算两个日期的相差天数

// 这里传参的日期格式为:xxxx/xx/xx xx:xx
_Day: function(start,end) {
    //将日期转为分隔符为-的格式
    let start_date = new Date(start.replace(/-/g, "/"));
    let end_date = new  Date(end.replace(/-/g, "/"));
    let days = end_date.getTime() - start_date.getTime();
    let day = parseInt(days / (1000 \* 60 \* 60 \* 24));
 },

获取昨日、今日、明日的日期

传参day:(-1:昨天,0:今天,1:明天;以此类推)
_getDay: function (day) {
    let today = new Date();
    let targetday = today.getTime() + 1000*60*60*24*day;
    today.setTime(targetday); //
    let tYear = today.getFullYear();
    let tMonth = today.getMonth() + 1;
    let tDate = today.getDate() < 10 ? '0' + today.getDate() : today.getDate();
    return tYear + "-" + tMonth + "-" + tDate;
},

推荐

挺全的日期插件:moment
时间戳在线转换工具:转换工具


万一呢
39 声望1 粉丝

努力学习进阶的小白一枚