String.Length
Function: This is actually an attribute of the String class, but since JavaScript treats both Full-width and Half-width as a single character, there may be a problem in some practical use, and now we are using prototype to make up for this deficiency.
Implementation method:
String.prototype.cnLength = function(){
var arr=this.match(/[^\x00-\xff]/ig);
return this.length+(arr==null?0:arr.length);
}
Test: Alert ("Easewe space Spaces". Cnlength ())-> display 16
Here are some methods of regular expressions and the coding principle of full-width characters.
Date.daydiff ()
Action: Calculates the time interval (year, month, Day, week) of two date-type variables
Implementation method:
Date.prototype.DayDiff = function(cDate,mode){
try{
cDate.getYear();
}catch(e){
return(0);
}
var base =60*60*24*1000;
var result = Math.abs(this - cDate);
switch(mode){
case "y":
result/=base*365;
break;
case "m":
result/=base*365/12;
break;
case "w":
result/=base*7;
break;
default:
result/=base;
break;
}
return(Math.floor(result));
}
Test: Alert (new Date ()). Daydiff (New Date (2002,0,1)))-> display 329
Alert (new Date ()). Daydiff (New Date (2002,0,1)), "M")-> display 10
Of course, it can be further expanded to get the response hours, minutes, or even seconds.