JS 时间戳转星期几 AND js时间戳判断时间几天前

话不多说,直接上代码。

function getWeek(date) {
	var week;
	if(date.getDay() == 0) week = "星期日"
	if(date.getDay() == 1) week = "星期一"
	if(date.getDay() == 2) week = "星期二"
	if(date.getDay() == 3) week = "星期三"
	if(date.getDay() == 4) week = "星期四"
	if(date.getDay() == 5) week = "星期五"
	if(date.getDay() == 6) week = "星期六"
	return week;
}

console.log(getWeek(new Date("2017-08-21")));

  

function getDateTimeBefor(publishtime) {
        var currTime = Date.parse(new Date());;
        var l = parseInt(currTime) - parseInt(publishtime);
        // 少于一分钟
        var time = l / 1000;
        if (time < 60) {
            return "刚刚";
        }

        // 秒转分钟
        var minuies = time / 60;
        if (minuies < 60) {
            return Math.floor(minuies) + "分钟前";
        }

        // 秒转小时
        var hours = time / 3600;
        if (hours < 24) {
            return Math.floor(hours) + "小时前";
        }
        //秒转天数
        var days = time / 3600 / 24;
        if (days < 30) {
            return Math.floor(days) + "天前";
        }
        //秒转月
        var months = time / 3600 / 24 / 30;
        if (months < 12) {
            return Math.floor(months) + "月前";
        }
        //秒转年
        var years = time / 3600 / 24 / 30 / 12;
        return Math.floor(years) + "年前";

    }

  

原文地址:https://www.cnblogs.com/RAINHAN/p/7406360.html