java 友好时间显示_js 显示友好的时间格式【刚刚、几秒前,几小时,几天前(3天内) 时间格式化】...

本文介绍两种将毫秒时间戳转换为易于理解的时间格式的方法。一种方法详细展示如何输出带有小时、分钟和秒的时间差;另一种则简化输出,如“几分钟前”。这两种方法适用于不同场景,帮助用户更直观地理解时间间隔。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**

* 毫秒转换友好的显示格式

* 输出格式:21小时28分钟15秒

* @param {[type]} time [description]

* @return {[type]} [description]

*/

function timeToDate(time)

{

// 获取当前时间戳

var currentTime = parseInt(new Date().getTime()/1000);

var diffTime = currentTime-time;

var second = 0;

var minute = 0;

var hour = 0;

if (null != diffTime && "" != diffTime) {

if (diffTime > 60 && diffTime < 60 * 60) {

diffTime = parseInt(diffTime / 60.0) + "分钟" + parseInt((parseFloat(diffTime / 60.0) - parseInt(diffTime / 60.0)) * 60) + "秒";

}

else if (diffTime >= 60 * 60 && diffTime < 60 * 60 * 24) {

diffTime = parseInt(diffTime / 3600.0) + "小时" + parseInt((parseFloat(diffTime / 3600.0) -

parseInt(diffTime / 3600.0)) * 60) + "分钟" +

parseInt((parseFloat((parseFloat(diffTime / 3600.0) - parseInt(diffTime / 3600.0)) * 60) -

parseInt((parseFloat(diffTime / 3600.0) - parseInt(diffTime / 3600.0)) * 60)) * 60) + "秒";

}

else {

//超过1天

var date = new Date(parseInt(time) * 1000);

diffTime = date.getFullYear()+"/"+(date.getMonth()+1)+"/"+date.getDate();

//diffTime = parseInt(diffTime) + "秒";

}

}

return diffTime;

}

毫秒转换友好的显示格式

/**

* 毫秒转换友好的显示格式

* 输出格式:21小时前

* @param {[type]} time [description]

* @return {[type]} [description]

*/

function dateStr(date){

//获取js 时间戳

var time=new Date().getTime();

//去掉 js 时间戳后三位,与php 时间戳保持一致

time=parseInt((time-date*1000)/1000);

//存储转换值

var s;

if(time<60*10){//十分钟内

return '刚刚';

}else if((time<60*60)&&(time>=60*10)){

//超过十分钟少于1小时

s = Math.floor(time/60);

return s+"分钟前";

}else if((time<60*60*24)&&(time>=60*60)){

//超过1小时少于24小时

s = Math.floor(time/60/60);

return s+"小时前";

}else if((time<60*60*24*3)&&(time>=60*60*24)){

//超过1天少于3天内

s = Math.floor(time/60/60/24);

return s+"天前";

}else{

//超过3天

var date= new Date(parseInt(date) * 1000);

return date.getFullYear()+"/"+(date.getMonth()+1)+"/"+date.getDate();

}

}

使用实例

//################# 使用实例 #######################

console.log(timeToDate(1475130065));

console.log(dateStr(1475130065));

time.js 插件 格式化时间戳

github : 链接描述

作者博客:链接描述

// Generated by CoffeeScript 1.7.1

(function(WIN) {

var DAY, DEFAULT_FORMAT, HOUR, MINUTE, MONTH, SECOND, YEAR, angularApp, entry, exports, getFullTime, map, replace, time, two, unify;

YEAR = "year";

MONTH = "month";

DAY = "day";

HOUR = "hour";

MINUTE = "minute";

SECOND = "second";

DEFAULT_FORMAT = "%y-%M-%d %h:%m:%s";

map = {

"%y": YEAR,

"%M": MONTH,

"%d": DAY,

"%h": HOUR,

"%m": MINUTE,

"%s": SECOND

};

unify = function(time) {

time -= 0;

if (("" + time).length === 10) {

time *= 1000;

}

return time;

};

two = function(str) {

var s;

s = "" + str;

if (s.length === 1) {

s = "0" + s;

}

return s;

};

replace = function(str, src, dst) {

var reg;

reg = new RegExp(src, "g");

return str.replace(reg, dst);

};

getFullTime = function(time) {

var date;

date = new Date(unify(time));

return {

year: date.getFullYear(),

month: two(date.getMonth() + 1),

day: two(date.getDate()),

hour: two(date.getHours()),

minute: two(date.getMinutes()),

second: two(date.getSeconds())

};

};

time = {

"default": function(time, format) {

var fullTime, ret, src;

if (format && (typeof format) !== "string") {

throw new Error("format must be a string.");

}

fullTime = getFullTime(time);

ret = format || DEFAULT_FORMAT;

for (src in map) {

ret = replace(ret, src, fullTime[map[src]]);

}

return ret;

},

human: function(time) {

var ago, curTime, diff, int;

time = unify(time);

int = parseInt;

curTime = +new Date();

diff = curTime - time;

ago = "";

if (1000 * 60 > diff) {

ago = "刚刚";

} else if (1000 * 60 <= diff && 1000 * 60 * 60 > diff) {

ago = int(diff / (1000 * 60)) + "分钟前";

} else if (1000 * 60 * 60 <= diff && 1000 * 60 * 60 * 24 > diff) {

ago = int(diff / (1000 * 60 * 60)) + "小时前";

} else if (1000 * 60 * 60 * 24 <= diff && 1000 * 60 * 60 * 24 * 30 > diff) {

ago = int(diff / (1000 * 60 * 60 * 24)) + "天前";

} else if (1000 * 60 * 60 * 24 * 30 <= diff && 1000 * 60 * 60 * 24 * 30 * 12 > diff) {

ago = int(diff / (1000 * 60 * 60 * 24 * 30)) + "月前";

} else {

ago = int(diff / (1000 * 60 * 60 * 24 * 30 * 12)) + "年前";

}

return ago;

}

};

entry = time["default"];

entry.human = entry.ago = time.human;

if (typeof module !== "undefined" && module.exports) {

return module.exports = exports = entry;

} else if (typeof WIN["define"] === "function") {

return define(function(require, exports, module) {

return module.exports = exports = function() {

return entry;

};

});

} else if (typeof WIN["angular"] === "object") {

angularApp = angular.module("binnng/time", []);

angularApp.factory("$time", function() {

return entry;

});

angularApp.filter("ago", function() {

return function(time) {

return entry.ago(time);

};

});

angularApp.filter("date", function() {

return function(time) {

return entry(time, "%y年%M月%d日");

};

});

return angularApp.filter("datetime", function() {

return function(time) {

return entry(time, DEFAULT_FORMAT);

};

});

} else {

return WIN["Time"] = entry;

}

})(window);

使用实例

newtime = + new Date;

console.log(Time(1473157947, "%y年%M月%d日%h时%m分%s秒"));

console.log(Time.human(1473157947));

console.log(Time.ago(1473157947));

// 输出

// 2016年09月06日18时32分27秒

// 23天前

// 23天前

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值