《造了个轮子》一篇文章读懂js-日期对象

轮子.png

1.基础

// 获取中国标准时间
var date = new Date(); //Wed Apr 22 2020 14:41:58 GMT+0800 (中国标准时间)
//方法一
var timestamp = (new Date()).getTime();
console.log(timestamp); //1495302061441
//方法二
var timestamp2 = (new Date()).valueOf();
console.log(timestamp2); //1495302061441
//方法三
var timestamp3 = Date.parse(new Date());
console.log(timestamp3); //1495302061000
//第一种和第二种是获取了当前毫秒的时间戳
//最后一种获取的时间戳是把毫秒改成000显示
var year = date.getFullYear().toString(); //年
var month = date.getMonth() + 1; //月
var day = date.getDate(); //日
var hour = date.getHours(); //时
var minute = date.getMinutes(); //分
var second = date.getSeconds(); //秒
console.log(year, month, day, hour, minute, second) //2020 4 22 14 57 8
console.log(year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second)

2. 获取当前时间转化格式为yyyy-MM-dd HH:mm:ss

console.log(year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second)//2020-05-21 00:00:00

3. 获取指定日期时间戳(两种方式)

console.log((new Date("2020-05-21 00:00:00")).getTime()/1000)//getTime()获取的为毫秒
console.log((new Date("2020-05-21 00:00:00")).getTime()/1000)

3. 知道时间戳转化格式为yyyy-MM-dd HH:mm:ss

function timestampToTime(timestamp) {
    console.log(timestamp.toString().length)
    var numLength = timestamp.toString().length;
    var date = '';
    if(numLength==10){ //时间戳为10位需*1000,时间戳为13位的话不需乘1000
        date = new Date(timestamp * 1000);
    }else if(numLength==13){
        date = new Date(timestamp);
    }
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = date.getMinutes() + ':';
    var s = date.getSeconds();
    return Y + M + D + h + m + s;
}
timestampToTime(1403058804);
console.log(timestampToTime(1589990400)); //2020-05-21 00:00:00

3. 日期格式转化为时间戳

var date = new Date('2020-05-21 00:00:00');
// 有三种方式获取
 var time1 = date.getTime();
var time2 = date.valueOf();
var time3 = Date.parse(date);
console.log(time1);//1589990400000
console.log(time2);//1589990400000
console.log(time3);//1589990400000

4. 知道(比如,评论的发布时间戳/日期格式) 显示几小时前,几天前,几个月前

function getTs(time) {
    var arr = time.split(/[- :]/),
        _date = new Date(arr[0], arr[1] - 1, arr[2], arr[3], arr[4], arr[5]),
        timeStr = Date.parse(_date)
    return timeStr
}
function handlePublishTimeDesc(post_modified) {
    // 拿到当前时间戳和发布时的时间戳,然后得出时间戳差
    var curTime = new Date();
    var postTime = new Date(post_modified);
    //上面一行代码可以换成以下(兼容性的解决)
    var timeDiff = curTime.getTime() - getTs(post_modified);
    // 单位换算
    var min = 60 * 1000;
    var hour = min * 60;
    var day = hour * 24;
    var week = day * 7;
    var month = week * 4;
    var year = month * 12;
    // 计算发布时间距离当前时间的周、天、时、分
    var exceedyear = Math.floor(timeDiff / year);
    var exceedmonth = Math.floor(timeDiff / month);
    var exceedWeek = Math.floor(timeDiff / week);
    var exceedDay = Math.floor(timeDiff / day);
    var exceedHour = Math.floor(timeDiff / hour);
    var exceedMin = Math.floor(timeDiff / min);
    // 判断
    if (exceedyear < 100 && exceedyear > 0) {
        return exceedyear + '年前';
    } else {
        if (exceedmonth < 12 && exceedmonth > 0) {
            return exceedmonth + '个月前';
        } else {
            if (exceedWeek < 4 && exceedWeek > 0) {
                return exceedWeek + '周前';
            } else {
                if (exceedDay < 7 && exceedDay > 0) {
                    return exceedDay + '天前';
                } else {
                    if (exceedHour < 24 && exceedHour > 0) {
                        return exceedHour + '小时前';
                    } else {
                        return exceedMin + '分钟前';
                    }
                }
            }
        }
    }
}
console.log(handlePublishTimeDesc("2020-03-26 15:33:00"))

4. 知道日期2020-03-26 15:33:00只显示2020-03-26

var timeStr = "2020-03-26 15:33:00";
console.log(timeStr.substring(0,10));
console.log(timeStr.slice(0,10));

你可能感兴趣的:(《造了个轮子》一篇文章读懂js-日期对象)