//- -------------------------------------------------- // Date formatting // Format YYYY/yyyy/YY/yy represents year // MM/M month // W/w week // dd/DD/ d/D Date // hh/HH/h/H Time // mm/m Minutes // ss/SS/s/S Seconds //------ --------------------------------------------- // Extension of Date, convert Date into String in the specified format // month (M), day (d), hour (h), minute (m), second (s), quarter (q) can use 1 -2 placeholders, // Year (y) can use 1-4 placeholders, milliseconds (S) can only use 1 placeholder (a number with 1-3 digits) // Example: // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 // (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 Date.prototype.Format = function(fmt) { //author: meizz var o = { "M " : this.getMonth() 1, //Month "d " : this.getDate(), //Day "h " : this.getHours(), //Hours "H " : this.getHours(), //Hours "m " : this.getMinutes(), //Minutes " s " : this.getSeconds(), //Seconds "q " : Math.floor((this.getMonth() 3)/3), //Quarter "S" : this.getMilliseconds() //Milliseconds }; if(/(y )/.test(fmt)) fmt=fmt.replace(RegExp.$1, (this.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; }
/** * Extension of Date, convert Date into String in specified format * Month (M), day (d), 12 hours (h), 24 hours (H), minutes (m), seconds ( s), week (E), quarter (q) can use 1-2 placeholders * year (y) can use 1-4 placeholders, milliseconds (S) can only use 1 placeholder symbol (is a number of 1-3 digits) * eg: * (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2006- 07-02 08:09:04.423 * (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 Tuesday 20:09:04 * (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04 * (new Date( )).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04 * (new Date()).pattern("yyyy- M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 */ Date.prototype.pattern=function(fmt) { var o = { "M " : this.getMonth() 1, //Month "d " : this.getDate(), //Day "h " : this.getHours() == 0 ? 12 : this.getHours( ), //hours "H " : this.getHours(), //hours "m " : this.getMinutes(), //minutes "s " : this.getSeconds() , //Seconds "q " : Math.floor((this.getMonth() 3)/3), //Quarter "S" : this.getMilliseconds() //Milliseconds }; var week = { "0" : "/u65e5", "1" : "/u4e00", "2" : "/u4e8c", "3" : "/u4e09", "4" : "/u56db", "5" : "/u4e94", "6" : "/u516d" }; if( /(y )/.test(fmt)){ fmt=fmt.replace(RegExp.$1, (this.getFullYear() "").substr(4 - RegExp.$1.length)); } if(/(E )/.test(fmt)){ fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "") week[this.getDay() ""]); } 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; }
/ /------------------------------------------------- -- //| Find the difference in days between two times. The date format is YYYY-MM-dd // ----------------------- ----------------------------- function daysBetween(DateOne,DateTwo) { var OneMonth = DateOne .substring(5,DateOne.lastIndexOf ('-')); var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-') 1); var OneYear = DateOne.substring( 0,DateOne.indexOf ('-'));
var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-')); var TwoDay = DateTwo.substring(DateTwo.length ,DateTwo.lastIndexOf ('-') 1); var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-')); var cha=((Date.parse(OneMonth '/' OneDay '/' OneYear)- Date.parse(TwoMonth '/' TwoDay '/' TwoYear))/86400000); return Math.abs(cha); }
// --------------------------------------------------- //| 日期计算 // --------------------------------------------------- Date.prototype.DateAdd = function(strInterval, Number) { var dtTmp = this; switch (strInterval) { case 's' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds() Number); //秒 case 'n' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes() Number, dtTmp.getSeconds()); //分 case 'h' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()), dtTmp.getDate(), dtTmp.getHours() Number, dtTmp.getMinutes(), dtTmp.getSeconds()); //时 case 'd' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()), dtTmp.getDate() Number, dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); //天 case 'w' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()), dtTmp.getDate() Number*7, dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); //周 case 'q' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());//季度 case 'm' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); //月 case 'y' :return new Date((dtTmp.getFullYear() Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); //年 } }
// --------------------------------------------------- //| 日期输出字符串,重载了系统的toString方法 // --------------------------------------------------- Date.prototype.toString = function(showWeek) { var myDate= this; var str = myDate.toLocaleDateString(); if (showWeek) { var Week = ['日','一','二','三','四','五','六']; str = ' 星期' Week[myDate.getDay()]; } return str; }
// --------------------------------------------------- //| 日期合法性验证 //| 格式为:YYYY-MM-DD或YYYY/MM/DD // --------------------------------------------------- function IsValidDate(DateStr) { var sDate=DateStr.replace(/(^s |s $)/g,''); //去两边空格; if(sDate=='') return true; //如果格式满足YYYY-(/)MM-(/)DD或YYYY-(/)M-(/)DD或YYYY-(/)M-(/)D或YYYY-(/)MM-(/)D就替换为'' //数据库中,合法日期可以是:YYYY-MM/DD(2003-3/21),数据库会自动转换为YYYY-MM-DD格式 var s = sDate.replace(/[d]{ 4,4 }[-/]{ 1 }[d]{ 1,2 }[-/]{ 1 }[d]{ 1,2 }/g,''); if (s=='') //说明格式满足YYYY-MM-DD或YYYY-M-DD或YYYY-M-D或YYYY-MM-D { var t=new Date(sDate.replace(/-/g,'/')); var ar = sDate.split(/[-/:]/); if(ar[0] != t.getYear() || ar[1] != t.getMonth() 1 || ar[2] != t.getDate()) { //alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。'); return false; } } else { //alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。'); return false; } return true; }
// ------------------ -------------------------------- //| Date and time check //| The format is: YYYY-MM-DD HH:MM:SS // ---------------------------------- ---------------- function CheckDateTime(str) { var reg = /^(d )-(d{ 1,2 })-( d{ 1,2 }) (d{ 1,2 }):(d{ 1,2 }):(d{ 1,2 })$/; var r = str.match(reg); if(r==null)return false; r[2]=r[2]-1; var d= new Date(r[1],r[2],r[3] ,r[4],r[5],r[6]); if(d.getFullYear()!=r[1])return false; if(d.getMonth()!=r [2])return false; if(d.getDate()!=r[3])return false; if(d.getHours()!=r[4])return false; if(d.getMinutes()!=r[5])return false; if(d.getSeconds()!=r[6])return false; return true; }
//------------------------------------------------ ------ //| Split the date into arrays // ---------------------------- ----------------------- Date.prototype.toArray = function() { var myDate = this; var myArray = Array(); myArray[0] = myDate.getFullYear(); myArray[1] = myDate.getMonth(); myArray[2] = myDate.getDate(); myArray[3] = myDate.getHours(); myArray[4] = myDate.getMinutes(); myArray[5] = myDate.getSeconds(); return myArray; }
// ------------------------------------------ ---------- //| Get date data information //| The parameter interval represents the data type //| y year m month d day w week ww week h hour n Minutes s seconds //--------------------------------------------- --------- Date.prototype.DatePart = function(interval) { var myDate = this; var partStr=''; var Week = [ '日','一','二','三','四','五','六']; switch (interval) { case 'y' :partStr = myDate.getFullYear();break; case 'm' :partStr = myDate.getMonth() 1;break; case 'd' :partStr = myDate.getDate();break; case ' w' :partStr = Week[myDate.getDay()];break; case 'ww' :partStr = myDate.WeekNumOfYear();break; case 'h' :partStr = myDate.getHours(); break; case 'n' :partStr = myDate.getMinutes();break; case 's' :partStr = myDate.getSeconds();break; } return partStr; }
// ---------------------------------------- ---------- //| Get the maximum number of days in the month of the current date // -------------------- ------------------------------- Date.prototype.MaxDayOfDate = function() { var myDate = this; var ary = myDate.toArray(); var date1 = (new Date(ary[0],ary[1] 1,1)); var date2 = date1. dateAdd(1,'m',1); var result = dateDiff(date1.Format('yyyy-MM-dd'),date2.Format('yyyy-MM-dd')); return result; }
// ---------------------------------------- --------------- //| Convert string to date type //| Format 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 delimCahar = DateStr.indexOf('/')!=-1?'/':'-'; var arys= DateStr.split( '-'); myDate = new Date(arys[0],--arys[1],arys[2]); } return myDate; }
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn