<script language="javascript"> /** * 将秒数换成时分秒格式 * 作者:龙周峰 */ function formatSeconds(value) { var secondTime = parseInt(value);// 秒 var minuteTime = 0;// 分 var hourTime = 0;// 小时 if(secondTime > 60) {//如果秒数大于60,将秒数转换成整数 //获取分钟,除以60取整数,得到整数分钟 minuteTime = parseInt(secondTime / 60); //获取秒数,秒数取佘,得到整数秒数 secondTime = parseInt(secondTime % 60); //如果分钟大于60,将分钟转换成小时 if(minuteTime > 60) { //获取小时,获取分钟除以60,得到整数小时 hourTime = parseInt(minuteTime / 60); //获取小时后取佘的分,获取分钟除以60取佘的分 minuteTime = parseInt(minuteTime % 60); } } var result = "" + parseInt(secondTime) + "秒"; if(minuteTime > 0) { result = "" + parseInt(minuteTime) + "分" + result; } if(hourTime > 0) { result = "" + parseInt(hourTime) + "小时" + result; } return result; } console.log(formatSeconds(10000002)) </script>
将秒数转化成 XX小时XX分XX秒
改革的用法:
Sub.timeTransf = function(e,data) { var time=e.data('time'); e.html(formatSeconds(time)); function formatSeconds(value) { var secondTime = parseInt(value); var minuteTime = 0; var hourTime = 0; if (secondTime > 60) { minuteTime = parseInt(secondTime / 60); secondTime = parseInt(secondTime % 60); if (minuteTime > 60) { hourTime = parseInt(minuteTime / 60); minuteTime = parseInt(minuteTime % 60); } } var result = "" + parseInt(secondTime); if (minuteTime > 0) { if(minuteTime<10){ result = "0" + parseInt(minuteTime) + ":" + result; }else{ result = "" + parseInt(minuteTime) + ":" + result; } } if (hourTime > 0) { result = "" + parseInt(hourTime) + ":" + result; } return result; } }
将秒数转化为 XX小时0X分0X秒
将时间搓转化为 2018-09-20 14:10:00这种格式的:
formatDateTime: function(inputTime) {
var date = new Date(inputTime);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
var h = date.getHours();
h = h < 10 ? ('0' + h) : h;
var minute = date.getMinutes();
var second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
return y+'-'+m+'-'+d+' '+' '+h+':'+minute+':'+second;
},
注意:这里的时间是毫秒,如果返回的时间是秒数的话,先转化为毫秒 (m*1000)
还可参考博客:https://www.cnblogs.com/zhanghaoliang/p/5997619.html
格式化时间:
//格式化时间
function ReloadPubdate(string) {
var re = /^(\d{2,4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
if (re.test(string)) {
var t = string.match(re);
var d = new Date(t[1], t[2] - 1, t[3], t[4], t[5], t[6]);
var c = new Date();
var s = (c.getTime() - d.getTime()) / 1000;
var m = Math.floor(s / 60);
var h = Math.floor(s / 3600);
var d = Math.floor(s / 86400);
var n = Math.floor(s / (86400 * 30));
var y = Math.floor(s / (86400 * 365));
if (y > 0) return y + "年以前";
if (n > 0) return n + "个月以前";
if (d > 0) return d + "天以前";
if (h > 0) return h + "小时以前";
if (m > 0) return m + "分钟以前";
}
return "刚刚";
}
传入的string格式是: console.log(ReloadPubdate("2018-10-10 16:55:56"))
适用场景: