【前端】跟当前时间比较,显示时间差距?

如图:

【前端】跟当前时间比较,显示时间差距?

将日期跟当前时间比较,并显示相差的时间如图展示,想封装一个方法,但是具体不知道该怎么弄好,求大神指点迷津。

自己在各路大神点思路下,自己给弄了一个,如下:

//与当前时间比较

function getDate(time){

var str = time;

//将字符串转换成时间格式

var timePublish = new Date(str);

var timeNow = new Date();

var minute = 1000 * 60;

var hour = minute * 60;

var day = hour * 24;

var month = day * 30;

var diffValue = timeNow - timePublish;

var diffMonth = diffValue / month;

var diffWeek = diffValue / (7 * day);

var diffDay = diffValue / day;

var diffHour = diffValue / hour;

var diffMinute = diffValue / minute;

if (diffValue < 0) {

console.log("错误时间");

}

else if (diffMonth > 3) {

result = timePublish.getFullYear()+"-";

result += (timePublish.getMonth() + 1) + "-";

result += timePublish.getDate();

// $.toptips(result);

}

else if (diffMonth > 1) {

result = parseInt(diffMonth) + "月前";

}

else if (diffWeek > 1) {

result = parseInt(diffWeek) + "周前";

}

else if (diffDay > 1) {

result = parseInt(diffDay) + "天前";

}

else if (diffHour > 1) {

result = parseInt(diffHour) + "小时前";

}

else if (diffMinute > 1) {

result = parseInt(diffMinute) + "分钟前";

}

else {

result = "刚刚";

}

return result;

}

回答

考虑下年、月、日、时、分的情况,主要是通过对比时间,分成六个级别【年、月、日、时、分、1分钟内】,哪个级别匹配到了就拿出结果即可:

function diffTime(dataStr){

var nowDate = new Date().getTime(),

oldDate = new Date(dataStr).getTime(),

diffTime = parseInt((nowDate - oldDate)/1000,10),

oneMinute = 60,

oneHour = 60 * oneMinute,

oneDay = 24 * oneHour,

oneMonth = 30 * oneDay,

oneYear = 12 * oneMonth,

compareArr = [oneYear,oneMonth,oneDay,oneHour,oneMinute],

postfix = ['年前','个月前','天前','个小时前','分钟前','1分钟内'],

diffYear,diffMonth,diffDay,diffHour,diffMinute,len=5;

for(var i =0; i< len ;i++){

var diff = Math.floor(diffTime/compareArr[i]);

if(diff > 0){

return diff + postfix[i];

}

else if(i === len -1 && diff === 0){

return postfix[len];

}

}

}

    var date1 = "2017-9-21 10:34"

function changeTime(date){

var timestamp1 = Date.parse(new Date(date))/1000;

var timestamp2 = Date.parse(new Date())/1000;

var time = timestamp2-timestamp1;

if(time<60){

return time+"秒前"

}else if(time>60&&time<3600){

var mins = Math.floor(time/60);

return mins+"分前"

}else if(time>3600&&time<86400){

var hours = Math.floor(time/3600);

return hours+"小时前"

}else if(time>86400){

var days = Math.floor(time/86400);

return days+"天前"

}

}

var time = changeTime(date1);

function diffTime(dateStr) {

const diff = new Date(new Date() - new Date(dateStr));

const year = diff.getUTCFullYear()-1970

const month = diff.getUTCMonth()

const day = diff.getUTCDate()-1;

const hours = diff.getUTCHours();

const minutes = diff.getUTCMinutes();

const seconds = diff.getUTCSeconds();

return {

year,

month,

day,

hours,

minutes,

seconds

}

}

//{year: 0, month: 0, day: 1, hours: 23, minutes: 58, seconds: 10}

console.log(diffTime('2017-09-25 22:25:08'))

直接给你代码就没意思了,给你提供个思路吧
主要说下是怎么得出这个时间差距的,通过前后两个时间戳相减得来的,比如说我要算 1506565621 这个时间戳和当前时间 1506565623 差距,直接把这个两个时间戳相减 得出的就是2,即为2秒前

依照这个思路,时间戳都是基于秒数来递增的,60s=1min,如果相差为60就为1分钟前,依次类推,一小时等于多少秒,将两个时间戳相减得到的数依次除以分钟、小时、天数对于的秒数,就可以判断为几秒前或者是几分钟前

以上是 【前端】跟当前时间比较,显示时间差距? 的全部内容, 来源链接: utcz.com/a/79198.html

回到顶部