//将秒转成文字描述: //参数: // secondOrigin:单位:秒,示例:"2700"或2700, // formatCode: // 值为0或null,返回中文时间格式,示例:"60"或60 返回 1分 // 值为1,返回数字时间格式:示例:"60"或60 返回 00:01 :00 //返回值: //1.formatCode == null 或 formatCode ==0 // secondOrigin = 2700或"2700",返回:45分 // secondOrigin = 2515或"2515",返回:41分54秒 // secondOrigin = 3960或"3960",返回:1小时6分 // secondOrigin = 60或"60",返回:1分 // //2.formatCode == 1 // secondOrigin = 2700或"2700",返回:00:45 :00 // secondOrigin = 2515或"2515",返回:00:41 :54 // secondOrigin = 3960或"3960",返回:01:06 :00 // secondOrigin = 60或"60",返回:00:01 :00 function secondToDateStr(secondOrigin, formatCode) { if (!hasValue(secondOrigin)) { return ""; } var result = ""; var hourStr = ""; var minuteStr = ""; var secondStr = ""; var milliSecondStr = ""; var timeInSecond = parseFloat(secondOrigin); var ss = 1000; var mi = ss * 60; var hh = mi * 60; var dd = hh * 24; var ms = timeInSecond * ss; var day = 0;//parseInt(ms / dd); var hour = parseInt((ms - day * dd) / hh); var minute = parseInt((ms - day * dd - hour * hh) / mi); var second = parseInt((ms - day * dd - hour * hh - minute * mi) / ss); var milliSecond = parseInt(ms - day * dd - hour * hh - minute * mi - second * ss); if (second > 59) { second = 0; minute += 1; } if (minute > 59) { minute = 0; hour += 1; } if (formatCode == null || formatCode == 0) { hourStr = (hour > 0) ? hour + "小时" : ""; minuteStr = (minute > 0) ? minute + "分" : ""; secondStr = (second > 0) ? second + "秒" : ""; milliSecondStr = (milliSecond > 0) ? milliSecond + "毫秒" : ""; if (timeInSecond >= 60 && timeInSecond < 60 * 60) { /*[1分钟,1小时)*/ result = minuteStr + secondStr; } else if (timeInSecond >= 60 * 60) { /*[1小时, ...)*/ result = hourStr + minuteStr; } else { /*(...,1分钟)*/ result = secondStr; } } else if (formatCode == 1) { hourStr = hour > 10 ? hour.toString() : 0 + hour.toString(); minuteStr = minute >= 10 ? minute.toString() : 0 + minute.toString(); secondStr = second >= 10 ? second.toString() : 0 + second.toString(); result = hourStr + ":" + minuteStr + ":" + secondStr; } return result; }
//计算指定时间与当前时间差,返回人性化的提示 //参数: // dateTimesString:时间格式:2015-05-18T00:00:00 function getDateDiffHumanTip (dateTimesString) { if (!hasValue(dateTimesString)) { return ""; } var result = ""; dateTimesString = dateTimesString.toString(); var minute = 1000 * 60; var hour = minute * 60; var day = hour * 24; var halfamonth = day * 15; var month = day * 30; var year = month * 12; //替换时间中的字符:T dateTimesString = dateTimesString.replace('T', ' '); //获取时间戳(距离1970-1-1的毫秒数) var dateTimeStamp = Date.parse(dateTimesString.replace(/-/gi, "/")); var now = new Date().getTime(); var diffValue = now - dateTimeStamp; if (diffValue < 0) { return dateTimesString.substr(0, 10); } var yearC = diffValue / year; var monthC = diffValue / month; var weekC = diffValue / (7 * day); var dayC = diffValue / day; var hourC = diffValue / hour; var minC = diffValue / minute; if (yearC > 1) { //result = parseInt(yearC) + "年前"; result = dateTimesString.substr(0, 10); } else if (monthC >= 1) { result = parseInt(monthC) + "月前"; } else if (weekC >= 1) { result = parseInt(weekC) + "星期前"; } else if (dayC >= 1) { result = parseInt(dayC) + "天前"; } else if (hourC >= 1) { result = parseInt(hourC) + "小时前"; } else if (minC >= 1) { result = parseInt(minC) + "分钟前"; } else result = "刚刚"; return result; }
//将 Date 对象转化为指定格式的String // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) //参数说明: // fmt --指定格式的格式字符串:例如:yyyy-MM-dd hh:mm:ss.S // date --日期对象(new Date()) //示例: //fmt为:yyyy-MM-dd hh:mm:ss.S 返回 2006-07-02 08:09:04.423 //fmt为:yyyy-M-d h:m:s.S 返回 2006-7-2 8:9:4.18 function getDateFomatString(date, fmt) { var o = { "M+": date.getMonth() + 1, //月份 "d+": date.getDate(), //日 "h+": date.getHours(), //小时 "m+": date.getMinutes(), //分 "s+": date.getSeconds(), //秒 "q+": Math.floor((date.getMonth() + 3) / 3), //季度 "S": date.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; }
来源:https://www.cnblogs.com/easy5weikai/p/4710326.html