話不多說,直接上代碼。
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) + "年前";
}
