首页 > 代码库 > JS中常用的几种时间格式处理-【笔记整理】

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

 

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

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

 1 //时间格式化函数
 2 Date.prototype.format = function (format) {
 3     var o = {
 4         "M+": this.getMonth() + 1, //month 
 5         "d+": this.getDate(), //day 
 6         "h+": this.getHours(), //hour 
 7         "m+": this.getMinutes(), //minute 
 8         "s+": this.getSeconds(), //second 
 9         "q+": Math.floor((this.getMonth() + 3) / 3), //quarter 
10         "S": this.getMilliseconds() //millisecond 
11     }
12     if (/(y+)/.test(format)) {
13         format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
14     }
15     for (var k in o) {
16         if (new RegExp("(" + k + ")").test(format)) {
17             format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
18         }
19     }
20     return format;
21 }

 

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

持续整理添加中

待续...

 

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