全面兼容的javascript时间格式化函数
全面兼容的javascript时间格式化函数,实用总结(并增加了将Unix 时间戳 转换为 时间的处理)!
/**
* 时间格式化
*
* @param strDateTime:需要格式化的字符串时间
* @param intType: 格式化类型
*
* @return string
*/
function formatDateTime(strDateTime, intType) {
var years, month, days, hours, minutes, seconds;
try {
if (strDateTime != undefined && strDateTime != null && strDateTime != "") {
var newDate = getJSDateFormString(strDateTime);
switch (Number(intType)) {
case 1: //格式:yyyy-MM-dd
years = newDate.getFullYear();
month = (newDate.getMonth() + 1);
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
newDate = years + "-" + month + "-" + days;
break;
case 2: //格式:MM-dd HH:mm
month = (newDate.getMonth() + 1);
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
newDate = month + "-" + days +
" " + hours + ":" + minutes;
break;
case 3: //格式:HH:mm:ss
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
seconds = newDate.getSeconds();
if (Number(seconds) < 10) seconds = "0" + seconds;
newDate = hours + ":" + minutes + ":" + seconds;
break;
case 4: //格式:HH:mm
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
newDate = hours + ":" + minutes;
break;
case 5: //格式:yyyy-MM-dd HH:mm
years = newDate.getFullYear();
month = (newDate.getMonth() + 1);
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
newDate = years + "-" + month + "-" + days +
" " + hours + ":" + minutes;
break;
case 6: //格式:yyyy/MM/dd
years = newDate.getFullYear();
month = (newDate.getMonth() + 1);
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
newDate = years + "/" + month + "/" + days;
break;
case 7: //格式:MM/dd HH:mm
month = (newDate.getMonth() + 1);
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
newDate = month + "/" + days +
" " + hours + ":" + minutes;
break;
case 8: //格式:yyyy/MM/dd HH:mm
years = newDate.getFullYear();
month = (newDate.getMonth() + 1);
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
newDate = years + "/" + month + "/" + days +
" " + hours + ":" + minutes;
break;
case 9: //格式:yy-MM-dd
years = newDate.getFullYear();
years = years.toString().substr(2, 2);
month = (newDate.getMonth() + 1);
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
newDate = years + "-" + month + "-" + days;
break;
case 10: //格式:yy/MM/dd
years = newDate.getFullYear();
years = years.toString().substr(2, 2);
month = (newDate.getMonth() + 1);
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
newDate = years + "/" + month + "/" + days;
break;
case 11: //格式:yyyy年MM月dd hh:mm:ss
years = newDate.getFullYear();
month = (newDate.getMonth() + 1);
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
seconds = newDate.getSeconds();
if (Number(seconds) < 10) seconds = "0" + seconds;
newDate = years + "年" + month + "月" + days +
" " + hours + ":" + minutes + ":" + seconds;
break;
case 12: //格式:yyyyMMdd
years = newDate.getFullYear();
month = (newDate.getMonth() + 1);
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
newDate = years.toString() + month.toString() + days.toString();
break;
case 13: //格式:MM-dd hh:mm:ss
month = (newDate.getMonth() + 1);
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
seconds = newDate.getSeconds();
if (Number(seconds) < 10) seconds = "0" + seconds;
newDate = month + "-" + days +
" " + hours + ":" + minutes + ":" + seconds;
break;
case 14: //格式:yyyy-MM-dd HH:mm:ss
years = newDate.getFullYear();
month = (newDate.getMonth() + 1);
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
seconds = newDate.getSeconds();
if (Number(seconds) < 10) seconds = "0" + seconds;
newDate = years + "-" + month + "-" + days +
" " + hours + ":" + minutes + ":" + seconds;
break;
}
}
} catch (e) {
newDate = new Date();
return newDate.getFullYear() + "-" +
(newDate.getMonth() + 1) + "-" +
newDate.getDate() + " " +
newDate.getHours() + ":" +
newDate.getMinutes() + ":" +
newDate.getSeconds();
}
return newDate;
}
扩充:
1、时间格式化2(与当前相差一天 就按格式的类型显示,否则就 显示为 "13小时前" 或 "35分钟前"等)
/**
* 时间格式化2
*
* @param strDateTime:需要格式化的字符串时间
* @param intType: 格式化类型
*
* @return string 与当前相差一天 就按格式的类型显示,否则就 显示为 "13小时前" 或 "35分钟前"等
*/
function formatDateTime2(strDateTime, intType){
if(strDateTime != null && strDateTime != ''){
var timeDiff = getTimediff(strDateTime,new Date());
if(timeDiff != null && timeDiff.length == 4){
if(timeDiff[0] > 0 || timeDiff[1] > 24)
return formatDateTime(strDateTime, intType);
else{
if(timeDiff[1] > 0)
return timeDiff[1] + "小时前";
else if(timeDiff[2] > 0)
return timeDiff[2] + "分钟前";
else
return timeDiff[3] + "秒前";
}
}else return '';
}else
return '';
}
2、根据字符串日期 获取 js Date 类型
/**
* 根据字符串日期 获取 js Date 类型
* @param strDateTime
*/
function getJSDateFormString(strDateTime){
var newDate, arrDate = new Array(), arrTime = new Array();
if(strDateTime != null && strDateTime != ""){
//获取日期和时间数组
if (strDateTime.toString().indexOf("-") != -1) {
var item = strDateTime.split(" ");
arrDate = item[0].toString().split("-");
arrTime = item[1].toString().split(":");
} else if (strDateTime.toString().indexOf("/") != -1) {
var item = strDateTime.split(" ");
arrDate = item[0].toString().split("/");
arrTime = item[1].toString().split(":");
} else if (!isNaN(Number(strDateTime))) {
//unix 时间戳
newDate = new Date(Number(strDateTime) * 1000);
return newDate;
}
//处理数据
if (arrDate != undefined && arrTime != undefined && arrDate.length == 3 && arrTime.length == 3 && (newDate == undefined || newDate == null)) {
newDate = new Date(
parseInt(arrDate[0]),
parseInt(Number(arrDate[1]) - 1),
parseInt(arrDate[2]),
parseInt(arrTime[0]),
parseInt(arrTime[1]),
parseInt(arrTime[2])
);
return newDate;
}
}else
return null;
}
3、获取两时间差
/**
* 获取两时间差
* @param beginDate
* @param endDate
*
* @return array() / null
*/
function getTimediff(beginDate,endDate){
if(beginDate != null && beginDate != '' && endDate != null && endDate != ''){
beginDate = typeof(beginDate) == "string"?getJSDateFormString(beginDate):beginDate;
endDate = typeof(endDate) == "string"?getJSDateFormString(endDate):endDate;
if(beginDate != null && endDate != null){
beginDate = Math.round(beginDate.getTime() / 1000); //转换为 unix 时间戳
endDate = Math.round(endDate.getTime() / 1000); //转换为 unix 时间戳
var timediff = Math.abs(endDate - beginDate);
var remain = timediff % 86400;
var hours = parseInt(remain / 3600);
remain = remain % 3600;
var mins = parseInt(remain / 60);
var secs = remain % 60;
return [
parseInt(timediff/86400), // day
hours, // hour
mins, // min
secs // sec
];
}else
return null;
}else
return null;
}
本文为方便工作总结而来,如有不足请指正!谢谢