/**
2 * 配列要素の取得 (プロトタイプ拡張またはオーバーロード)
3 * @param {o} 取得した要素の値
4 * @type int
5 * @returns 要素インデックス
6*/
7 Array.prototype.contains = function(o) {
8 varindex = -1;
9 for(var i=0;ireturnindex;
}
/**
* 日付の形式 (プロトタイプ拡張またはオーバーロード)
* 形式 YYYY/yyyy/YY/yy は年を表します
* MM/M 月
* W/w 週
* dd/ DD/ d/D 日付
* hh/HH/h/H 時刻
* mm/m 分
* ss/SS/s/S 秒
* @param {formatStr} 形式テンプレート
* @type string
* @returns 日付文字列
*/
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());
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());
文字列を返します。
}
/**
* 日付の違いを比較します (プロトタイプ拡張またはオーバーロード)
* @param {strInterval} 日付型: 'y, m, d, h, n, s, w'
* @param {dtEnd} format は日付型または有効な日付形式文字列です
* @type int
* @returns 比較結果
*/
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.getFull Year()-dtStart.getFull Year())*12) - (dtStart.getMonth() 1);
case 'y' :return dtEnd.getFull Year() - dtStart.getFull Year();
}
}
/**
* 日付計算 (プロトタイプ拡張またはオーバーロード)
* @param {strInterval} 日付型: 'y, m, d, h, n, s, w'
* @param {Number} 数量
* @type Date
* @returns 計算された日付
*/
Date.prototype.dateAdd = function(strInterval, Number) {
var dtTmp = this;
switch (strInterval) {
case 's' :return new Date(Date.parse(dtTmp) (1000 * Number));
case 'n' :return new Date(Date.parse(dtTmp) (60000 * Number));
case 'h' :return new Date(Date.parse(dtTmp) (3600000 * Number));
case 'd' :return new Date(Date.parse(dtTmp) (86400000 * Number));
case 'w' :return new Date(Date.parse(dtTmp) ((86400000 * 7) * Number));
case 'q' :return new Date(dtTmp.getFull Year(), (dtTmp.getMonth()) Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds ());
case 'm' :return new Date(dtTmp.getFull Year(), (dtTmp.getMonth()) Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds() );
case 'y' :return new Date((dtTmp.getFull Year() Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds() );
}
}
/**
* 日付データ情報の取得 (プロトタイプ拡張またはオーバーロード)
* @param {interval} 日付型: 'y, m, d, h, n, s, w'
* @type int
* @指定された日付部分を返します
*/
Date.prototype.datePart = function(interval){
var myDate = this;
varpartStr='';
var Week = ['日','一','二','三','四','五','六'];
スイッチ (間隔)
{
case 'y' :partStr = myDate.getFull Year();break;
case 'm' :partStr = myDate.getMonth() 1;break;
ケース 'd' :partStr = myDate.getDate();break;
case 'w' :partStr = Week[myDate.getDay()];break;
ケース 'ww' :partStr = myDate.WeekNumOf Year();break;
ケース 'h' :partStr = myDate.getHours();break;
case 'n' :partStr = myDate.getMinutes();break;
case 's' :partStr = myDate.getSeconds();break;
}
partStr を返します。
}
/**
* Split the date into an array (prototype extension or overload)
* @type array
* @returns date array
*/
Date.prototype.toArray = function() {
var myDate = this;
var myArray = Array();
myArray[0] = myDate.getFullYear();
myArray[1] = myDate.getMonth() 1;
myArray[2] = myDate.getDate();
myArray[3] = myDate.getHours();
myArray[4] = myDate.getMinutes();
myArray[5] = myDate.getSeconds();
return myArray;
}
/**
* Get the number of days in the current month (prototype extension or overload)
* @type int
* @returns the number of days
*/
Date.prototype.daysOfMonth = function(){
var myDate = this;
var ary = myDate.toArray();
var date1 = (new Date(ary[0],ary[1] 1,1));
var date2 = date1.dateAdd('m',1);
var result = daysDiff(date1.format('yyyy-MM-dd'),date2.format('yyyy-MM-dd'));
return result;
}
/**
* Determine leap year (prototype extension or overloading)
* @type boolean
* @returns whether it is a leap year true/false
*/
Date.prototype.isLeapYear = function() {
return (0==this.getYear()%4&&((this.getYear()0!=0)||(this.getYear()@0==0)));
}
/**
* Compare the difference in days between two dates (customized)
* @param {DateOne} Date one
* @param {DateOne} Date two
* @type int
* @returns Comparison results
*/
function daysDiff(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 calculation (custom)
* @param {strInterval} Date type: 'y, m, d, h, n, s, w'
* @param {Number} quantity
* @param {prmDate} original date
* @type Date
* @returns calculated date
*/
function dateAdd(interval,number,prmDate){
number = parseInt(number);
if (typeof(prmDate)=="string"){
prmDate = prmDate.split(/D/);
--prmDate[1];
eval("var prmDate = new Date(" prmDate.join(",") ")");
}
if (typeof(prmDate)=="object"){
var prmDate = prmDate
}
switch(interval){
case "y": prmDate.setFullYear(prmDate.getFullYear() number); break;
case "m": prmDate.setMonth(prmDate.getMonth() number); break;
case "d": prmDate.setDate(prmDate.getDate() number); break;
case "w": prmDate.setDate(prmDate.getDate() 7*number); break;
case "h": prmDate.setHours(prmDate.getHour() number); break;
case "n": prmDate.setMinutes(prmDate.getMinutes() number); break;
case "s": prmDate.setSeconds(prmDate.getSeconds() number); break;
case "l": prmDate.setMilliseconds(prmDate.getMilliseconds() number); break;
}
return prmDate;
}
/**
* Get the string length (prototype extension or overload)
* @type int
* @returns String length
*/
String.prototype.len = function() {
var arr=this.match(/[^x00-xff]/ig);
return this.length (arr==null?0:arr.length);
}
/**
* Get the specified number of characters from the left of the string (prototype expansion or overloading)
* @param {num} Get the number
* @type string
* @returns Matched string
*/
String.prototype.left = function(num,mode) {
if(!/d /.test(num)) return(this);
var str = this.substr(0,num);
if(!mode) return str;
var n = str.len() - str.length;
num = num - parseInt(n/2);
return this.substr(0,num);
}
/**
* Get the specified number of characters from the right side of the string (prototype expansion or overloading)
* @param {num} Get the number
* @type string
* @returns Matched string
*/
String.prototype.right = function(num,mode) {
if(!/d /.test(num)) return(this);
var str = this.substr(this.length-num);
if(!mode) return str;
var n = str.len() - str.length;
num = num - parseInt(n/2);
return this.substr(this.length-num);
}
/**
* String contains (prototype extension or overload)
* @param {string: str} The substring to search for
* @param {bool: mode} Whether to ignore case
* @type int
* @returns the number of matches
*/
String.prototype.matchCount = function(str,mode) {
return eval("this.match(/(" str ")/g" (mode?"i":"") ").length");
}
/**
* Remove left and right spaces (prototype expansion or overloading)
* @type string
* @returns processed string
*/
String.prototype.trim = function() {
return this.replace(/(^s*)|(s*$)/g,"");
}
/**
* Remove left spaces (prototype expansion or overloading)
* @type string
* @returns processed string
*/
String.prototype.lTrim = function() {
return this.replace(/(^s*)/g, "");
}
/**
* 右側のスペースを削除します (プロトタイプ展開またはオーバーロード)
* @type string
* @returns 処理された文字列
*/
String.prototype.rTrim = function() {
return this.replace(/(s*$)/g, "");
}
/**
* 文字列を日付型に変換します (プロトタイプ拡張またはオーバーロード)
* @type Date
* @returns Date
*/
String.prototype.toDate = function() {
var Converted = Date.parse(this);
var myDate = 新しい日付(変換済み);
if (isNaN(myDate)) {var arys= this.split('-'); myDate = 新しい日付(arys[0],--arys[1],arys[2]);
myDate を返します。
}