写一个模块,外部调用这个模块,请求参数是时间戳,模块输出要求如下
1,今天的时间,用24小时制写
2,昨天的时间,统一写昨天
3,昨天之前的时间,但是在本周之内的时间,统一用周一到周日表示
4,上周的时间统一写成18/4/16,分别代表年/月/日
5,当时间为个位数的时候需要用0来补位,年月日不用补位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<script>
var a = new Date("2018-4-20 23:50:48").getTime()
function setTime(date){
//获取当前时间
var today = new Date();
var nowTime = today.getTime();
var inputTime = new Date(date);
//首先的到今天凌晨的时间的毫秒数
var lc = new Date(today.getFullYear(),today.getMonth(),today.getDate()).getTime();
/*当天的时间内*/
if(lc < date){//输入的时间毫秒数大于今天凌晨的时间毫秒数就是今天
var h = inputTime.getHours()
var m = inputTime.getMinutes()
var s = inputTime.getSeconds()
h<10?'0'+h:h;
m<10?'0'+m:m;
s<10?'0'+s:s;
return h+":"+m+":"+s
}
var disdays = parseInt((nowTime - date)/(1000*60*60*24));
/*如果是一周之内的*/
if(disdays<7){
/*昨天的时间*/
//在获取昨天凌晨的时间的毫秒数
var yestdaylc = new Date(lc-24*60*60*1000).getTime();
if(date>=yestdaylc&&date<lc){
var day = inputTime.getDay()
var h = inputTime.getHours()
var m = inputTime.getMinutes()
var s = inputTime.getSeconds()
h<10?'0'+h:h;
m<10?'0'+m:m;
s<10?'0'+s:s;
return "昨天 "+h+":"+m+":"+s
}else{
var day = inputTime.getDay()
switch(Number(day)){
case 0:
day = "日"
break ;
case 1:
day = "一"
break ;
case 2:
day = "二"
break ;
case 3:
day = "三"
break ;
case 4:
day = "四"
break ;
case 5:
day = "五"
break ;
case 6:
day = "六"
break ;
}
var h = inputTime.getHours()
var m = inputTime.getMinutes()
var s = inputTime.getSeconds()
h<10?'0'+h:h;
m<10?'0'+m:m;
s<10?'0'+s:s;
return "星期"+day+" "+h+":"+m+":"+s
}
}else{
var year = inputTime.getFullYear()
var month = inputTime.getMonth()+1
var dayte = inputTime.getDate()
h<10?'0'+h:h;
m<10?'0'+m:m;
s<10?'0'+s:s;
return year+"/"+month+"/"+dayte
}
}
console.log(setTime(a))
</script>
</body>
</html>
网友评论