格式化${startTime}距现在的已过时间
/**
* @desc 格式化${startTime}距现在的已过时间
* @param {Date} startTime
* @return {String}
*/
function formatPassTime(startTime) {
var currentTime = Date.parse(new Date()),
time = currentTime - startTime,
day = parseInt(time / (1000 * 60 * 60 * 24)),
hour = parseInt(time / (1000 * 60 * 60)),
min = parseInt(time / (1000 * 60)),
month = parseInt(day / 30),
year = parseInt(month / 12);
if (year) return year + "年前"
if (month) return month + "个月前"
if (day) return day + "天前"
if (hour) return hour + "小时前"
if (min) return min + "分钟前"
else return '刚刚'
}
?格式化现在距${endTime}的剩余时间
/**
*
* @desc 格式化现在距${endTime}的剩余时间
* @param {Date} endTime
* @return {String}
*/
function formatRemainTime(endTime) {
var startDate = new Date(); //开始时间
var endDate = new Date(endTime); //结束时间
var t = endDate.getTime() - startDate.getTime(); //时间差
var d = 0,
h = 0,
m = 0,
s = 0;
if (t >= 0) {
d = Math.floor(t / 1000 / 3600 / 24);
h = Math.floor(t / 1000 / 60 / 60 % 24);
m = Math.floor(t / 1000 / 60 % 60);
s = Math.floor(t / 1000 % 60);
}
return d + "天 " + h + "小时 " + m + "分钟 " + s + "秒";
}
?计算${startTime - endTime}的剩余时间
/**
* @desc ${startTime - endTime}的剩余时间,startTime大于endTime时,均返回0
* @param { Date | String } startTime
* @param { Date | String } endTime
* @returns { Object } { d, h, m, s } 天 时 分 秒
*/
function timeLeft(startTime, endTime) {
if (!startTime || !endTime) {
return
}
var startDate,endDate;
if (startTime instanceof Date) {
startDate = startTime;
} else {
startDate = new Date(startTime.replace(/-/g, '/')) //开始时间
}
if (endTime instanceof Date) {
endDate = endTime;
} else {
endDate = new Date(endTime.replace(/-/g, '/')) //结束时间
}
var t = endDate.getTime() - startDate.getTime()
var d = 0,
h = 0,
m = 0,
s = 0
if (t >= 0) {
d = Math.floor(t / 1000 / 3600 / 24)
h = Math.floor(t / 1000 / 60 / 60 % 24)
m = Math.floor(t / 1000 / 60 % 60)
s = Math.floor(t / 1000 % 60)
}
return { d, h, m, s }
}
获取指定日期月份的总天数
/**
* @desc 获取指定日期月份的总天数
* @param {Date} time
* @return {Number}
*/
function monthDays(time){
time = new Date(time);
var year = time.getFullYear();
var month = time.getMonth() + 1;
return new Date(year, month, 0).getDate();
}
时间日期格式转换
Date.prototype.Format = function(formatStr) {
var str = formatStr;
var Week = ['日', '一', '二', '三', '四', '五', '六'];
str = str.replace(/yyyy|YYYY/, this.getFullYear());
str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
str = str.replace(/MM/, (this.getMonth() + 1) > 9 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1));
str = str.replace(/M/g, (this.getMonth() + 1));
str = str.replace(/w|W/g, Week[this.getDay()]);
str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
str = str.replace(/d|D/g, this.getDate());
str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
str = str.replace(/h|H/g, this.getHours());
str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
str = str.replace(/m/g, this.getMinutes());
str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
str = str.replace(/s|S/g, this.getSeconds());
return str
}
随机数时间戳
function uniqueId(){
var a=Math.random,b=parseInt;
return Number(new Date()).toString()+b(10*a())+b(10*a())+b(10*a());
}
另一种正则日期格式化函数+调用方法
Date.prototype.format = function(format){ //author: meizz
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
(this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)if(new RegExp("("+ k +")").test(format))
format = format.replace(RegExp.$1,
RegExp.$1.length==1 ? o[k] :
("00"+ o[k]).substr((""+ o[k]).length));
return format;
}
alert(new Date().format("yyyy-MM-dd hh:mm:ss"));
时间个性化输出功能
/*
1、< 60s, 显示为“刚刚”
2、>= 1min && < 60 min, 显示与当前时间差“XX分钟前”
3、>= 60min && < 1day, 显示与当前时间差“今天 XX:XX”
4、>= 1day && < 1year, 显示日期“XX月XX日 XX:XX”
5、>= 1year, 显示具体日期“XXXX年XX月XX日 XX:XX”
*/
function timeFormat(time){
var date = new Date(time)
, curDate = new Date()
, year = date.getFullYear()
, month = date.getMonth() + 1
, day = date.getDate()
, hour = date.getHours()
, minute = date.getMinutes()
, curYear = curDate.getFullYear()
, curHour = curDate.getHours()
, timeStr;
if(year < curYear){
timeStr = year +'年'+ month +'月'+ day +'日 '+ hour +':'+ minute;
}else{
var pastTime = curDate - date
, pastH = pastTime/3600000;
if(pastH > curHour){
timeStr = month +'月'+ day +'日 '+ hour +':'+ minute;
}else if(pastH >= 1){
timeStr = '今天 ' + hour +':'+ minute +'分';
}else{
var pastM = curDate.getMinutes() - minute;
if(pastM > 1){
timeStr = pastM +'分钟前';
}else{
timeStr = '刚刚';
}
}
}
return timeStr;
}
|