JS中常用的几种时间格式处理-【笔记整理】

时间:2021-01-23 13:04:34

//此处整理点平时常用到的时间格式处理方法

-------------------------------------------

 //时间格式化函数
Date.prototype.format = function (format) {
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;
}
 //可将  var  time = new date();转换成 2016-08-03 18:30:00 格式//消除浏览器之间差异
function DateHandle(objDate) {
objDate = new Date(); //创建一个日期对象表示当前时间
var year = objDate.getFullYear(); //四位数字年
var month = objDate.getMonth() + 1; //getMonth()返回的月份是从0开始的,还要加1
var date = objDate.getDate();
var hours = objDate.getHours();
var minutes = objDate.getMinutes();
var seconds = objDate.getSeconds();
var date = year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds;
return date;
}
 //+---------------------------------------------------
//| 字符串转成日期类型
//| 格式 MM/dd/YYYY MM-dd-YYYY YYYY/MM/dd YYYY-MM-dd
//+---------------------------------------------------
function StringToDate(DateStr) { var converted = Date.parse(DateStr);
var myDate = new Date(converted);
if (isNaN(myDate)) {
var arys = DateStr.split('-');
myDate = new Date(arys[0], --arys[1], arys[2]);
}
return myDate;
}
 //+---------------------------------------------------
//| 比较日期差 dtEnd 格式为日期型或者有效日期格式字符串
//+---------------------------------------------------
Date.prototype.DateDiff = function (strInterval, dtEnd) {
var dtStart = this;
if (typeof dtEnd == 'string')//如果是字符串转换为日期型
{
dtEnd = StringToDate(dtEnd);
}
switch (strInterval) {
case 's': return parseInt((dtEnd - dtStart) / 1000);
case 'n': return parseInt((dtEnd - dtStart) / 60000);
case 'h': return parseInt((dtEnd - dtStart) / 3600000);
case 'd': return parseInt((dtEnd - dtStart) / 86400000);
case 'w': return parseInt((dtEnd - dtStart) / (86400000 * 7));
case 'm': return (dtEnd.getMonth() + 1) + ((dtEnd.getFullYear() - dtStart.getFullYear()) * 12) - (dtStart.getMonth() + 1);
case 'y': return dtEnd.getFullYear() - dtStart.getFullYear();
}
}
 //时间转换//加数字颜色/样式
  //time 单位 秒
function secondToDateAndStyle(time) {
time = parseInt(time);
if (null != time && "" != time) {
if (time > 60 && time < 60 * 60) {
time = "<span style=\"color:#FF6600;\">" + parseInt(time / 60.0) + "</span>" + "分" + "<span style=\"color:#FF6600;\">" + parseInt((parseFloat(time / 60.0) - parseInt(time / 60.0)) * 60) + "</span>" + "秒";
} else if (time >= 60 * 60 && time < 60 * 60 * 24) {
time = "<span style=\"color:#FF6600;\">" + parseInt(time / 3600.0) + "</span>" + "时" + "<span style=\"color:#FF6600;\">" + parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) + "</span>" + "分" + "<span style=\"color:#FF6600;\">" + parseInt((parseFloat((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) - parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60)) * 60) + "</span>" + "秒";
} else {
time = "<span style=\"color:#FF6600;\">" + parseInt(time) + "</span>" + "秒";
}
} else {
time = "<span style=\"color:#FF6600;\">0</span>秒";
}
return time;
}
 //时间比较函数
function CompareTime(Time1, Time2) {
var start = new Date(Time1.replace("-", "/").replace("-", "/"));
var end = new Date(Time2.replace("-", "/").replace("-", "/"));
if (end <= start) {
//后者小于前者返回false
return false;
}
return true;//后者大于前者返回true
}

持续整理添加中

待续...