摘要:毫秒转换友好的显示格式输出格式小时分钟秒获取当前时间戳分钟秒小时分钟秒超过天秒毫秒转换友好的显示格式毫秒转换友好的显示格式输出格式小时前获取时间戳去掉时间戳后三位,与时间戳保持一致存储转换值十分钟内刚刚超过十分钟少于小时分钟前
/**
* 毫秒转换友好的显示格式
* 输出格式: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);
使用实例
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/80578.html
摘要:前言经过将近一个多月的开发我们团队开发的微信小程序出发吧一起终于开发完成现在的线上版本为版本文章主要介绍该小程序在开发中所用到的技术已经在开发中遇到问题的采取的解决方法开源地址开发中技术问题汇总使用的出现问题在小程序开发过程中我们经常会用 前言 经过将近一个多月的开发,我们团队开发的微信小程序 出发吧一起 终于开发完成,现在的线上版本为 2.2.4-beta 版本文章主要介绍该小程序...
摘要:时间戳转字符串格式邱先生烟火里的尘埃版本传入时间戳获取时间戳去掉时间戳后三位,与时间戳保持一致存储转换值十分钟内刚刚超过十分钟少于小时分钟前超过小时少于小时小时前超过天少于天内天前超过天 应用场景 浏览实时信息网站时,总会看到发布时间,是这么显示的 例如 刚刚、几秒前,几分钟,几天,日期 ...,提供以下处理方案 服务端 ——PHP 客户端 ——JavaScript showI...
摘要:时间戳转字符串格式邱先生烟火里的尘埃版本传入时间戳获取时间戳去掉时间戳后三位,与时间戳保持一致存储转换值十分钟内刚刚超过十分钟少于小时分钟前超过小时少于小时小时前超过天少于天内天前超过天 应用场景 浏览实时信息网站时,总会看到发布时间,是这么显示的 例如 刚刚、几秒前,几分钟,几天,日期 ...,提供以下处理方案 服务端 ——PHP 客户端 ——JavaScript showI...
摘要:经常在朋友圈,空间微博上看到动态的发布时间评论时间,都显示,昨天,前天,几天前,比起直接显示几月几日几分几秒要优雅的多。获取已经过了多久时间转换刚刚几分钟前几小时前今天昨天前天几天前时间戳今天最大时间刚刚分钟前小时前今天昨天前天天前原文 经常在朋友圈,QQ空间、微博上看到动态的发布时间、评论时间,都显示,昨天,前天,几天前,比起直接显示几月几日几分几秒要优雅的多。 于是自己的项目也想采...
摘要:例子毫秒个月前年前源码算时间差历史时间戳,必传当前时间戳,不传将获取当前时间戳年前个月前周前天前个小时前分钟前刚刚改成了型参数,应该是这样吧算时间差历史时间戳,必传当前时间戳,不传将获取当前时间戳年前个月前周前天前个小时前分钟前刚刚 刚好项目需要这样一个功能,顺便共享出来给大家玩耍。 https://github.com/jaywcjlove/date.js 例子: dateDiff(...
阅读 3233·2021-11-23 09:51
阅读 1601·2021-11-22 09:34
阅读 2896·2021-10-27 14:15
阅读 2369·2021-10-12 10:17
阅读 1987·2021-10-12 10:12
阅读 1008·2021-09-27 14:00
阅读 2049·2021-09-22 15:19
阅读 1077·2019-08-30 10:51