将年月日转换为-几个小时前之类的
传入的时间是字符串类型
方法一
点击查看代码
function timeFormat(time)
{
var nowTime = new Date();
var day = nowTime.getDate();
var hours = parseInt(nowTime.getHours());
var minutes = nowTime.getMinutes();
// 开始分解付入的时间
var timeday = time.substring(8, 10);
var timehours = parseInt(time.substring(11, 13));
var timeminutes = time.substring(14, 16);
var d_day = Math.abs(day - timeday);
var d_hours = hours - timehours;
var d_minutes = Math.abs(minutes - timeminutes);
if (d_day <= 1) {
switch (d_day) {
case 0: if (d_hours == 0 && d_minutes > 0) {
return d_minutes + '分钟前';
} else if (d_hours == 0 && d_minutes == 0) {
return '1分钟前';
} else {
return d_hours + '小时前';
}
break;
case 1: if (d_hours < 0) { return (24 + d_hours) + '小时前'; } else { return d_day + '天前'; } break; } }
else if (d_day > 1 && d_day < 10) { return d_day + '天前'; } else { return time; }
}
console.log(timeFormat("2020-06-21 22:1:2"));
方法二
点击查看代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
var pretime = "2018-04-07 14:38:16";
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var halfamonth = day * 15;
var month = day * 30;
//这是第一次打开页面时调用
getDate(pretime);
//然后再每隔一分钟更新一次时间
setInterval(function() {
getDate(pretime);
}, 60000);
function getDate(dateTimeStamp){
if(dateTimeStamp==undefined){
return false;
}else{
dateTimeStamp = dateTimeStamp.replace(/\-/g, "/");
var sTime = new Date(dateTimeStamp).getTime();//把时间pretime的值转为时间戳
var now = new Date().getTime();//获取当前时间的时间戳
var diffValue = now - sTime;
if(diffValue < 0){
console.log("结束日期不能小于开始日期!");
}
var monthC =diffValue/month;
var weekC =diffValue/(7*day);
var dayC =diffValue/day;
var hourC =diffValue/hour;
var minC =diffValue/minute;
if(monthC>=1){
console.log(parseInt(monthC) + "个月前");
}
else if(weekC>=1){
console.log(parseInt(weekC) + "周前")
}
else if(dayC>=1){
console.log(parseInt(dayC) +"天前")
}
else if(hourC>=1){
console.log(parseInt(hourC) +"个小时前")
}
else if(minC>=1){
console.log(parseInt(minC) +"分钟前")
}else{
console.log("刚刚")
}
}
}
</script>
</body>
</html>
方法三
最近在做项目的时候,需要把后台返回的时间转换成几秒前、几分钟前、几小时前、几天前等的格式;后台返回的时间格式为:2015-07-30 09:36:10,需要根据当前的时间与返回的时间进行对比,最后显示成几秒前、几分钟前、几小时前、几天前的形式。
1.由于返回的时间是字符串格式,所以要先转换成时间戳
//字符串转换为时间戳
function getDateTimeStamp (dateStr) {
return Date.parse(dateStr.replace(/-/gi,"/"));
}
2.将返回的时间戳与当前时间戳进行比较,转换成几秒前、几分钟前、几小时前、几天前的形式。
点击查看代码
function getDateDiff (dateStr) {
var publishTime = getDateTimeStamp(dateStr)/1000,
d_seconds,
d_minutes,
d_hours,
d_days,
timeNow = parseInt(new Date().getTime()/1000),
d,
date = new Date(publishTime*1000),
Y = date.getFullYear(),
M = date.getMonth() + 1,
D = date.getDate(),
H = date.getHours(),
m = date.getMinutes(),
s = date.getSeconds();
//小于10的在前面补0
if (M < 10) {
M = '0' + M;
}
if (D < 10) {
D = '0' + D;
}
if (H < 10) {
H = '0' + H;
}
if (m < 10) {
m = '0' + m;
}
if (s < 10) {
s = '0' + s;
}
d = timeNow - publishTime;
d_days = parseInt(d/86400);
d_hours = parseInt(d/3600);
d_minutes = parseInt(d/60);
d_seconds = parseInt(d);
if(d_days > 0 && d_days < 3){
return d_days + '天前';
}else if(d_days <= 0 && d_hours > 0){
return d_hours + '小时前';
}else if(d_hours <= 0 && d_minutes > 0){
return d_minutes + '分钟前';
}else if (d_seconds < 60) {
if (d_seconds <= 0) {
return '刚刚发表';
}else {
return d_seconds + '秒前';
}
}else if (d_days >= 3 && d_days < 30){
return M + '-' + D + ' ' + H + ':' + m;
}else if (d_days >= 30) {
return Y + '-' + M + '-' + D + ' ' + H + ':' + m;
}
}
3.使用方法:
dateStr:返回的时间字符串,格式如:2015-07-30 09:36:10
// 转换后的结果 var str = getDateDiff(dateStr); // 在控制台输出结果 console.log(str);