In Asp.net project development, JavaScript applications are often used. From today on, I will write some frequently used methods in project development.
(1) convert a date
Yyyy/mm/DD format date
1 //------------------------------------------------------------
2 // convert the date to yyyy/mm/DD format
3 // parameter: appropriate date
4
5 // Date: yyyy/mm/DD format date
6
7 function convfazydate (sdate ){
8 var retdate = "";
9 var ymd;
10 if (isnan (sdate )){
11 retdate = sdate. Replace (/\ D/g ,"/");
12 var TMP = retdate. Split ("/");
13 if (TMP. Length <3) Return "";
14 ymd = TMP. Slice (0, 3 );
15} else {
16 var Len = sdate. length;
17 if (LEN <5) Return "";
18 ymd = new array (
19 sdate. substr (0, len-4 ),
20 sdate. substr (LEN-4, 2 ),
21 sdate. substr (LEN-2, 2)
22 );
23}
24 if (ymd [0]. length = 0) | (ymd [1]. length = 0) | (ymd [2]. length = 0) Return "";
25 if (ymd [0]. Length = 1) ymd [0] = "200" + ymd [0];
26 if (ymd [0]. Length = 2) ymd [0] = "20" + ymd [0];
27 if (ymd [0]. Length = 3) ymd [0] = "2" + ymd [0];
28 If (ymd [1]. Length = 1) ymd [1] = "0" + ymd [1];
29 If (ymd [2]. Length = 1) ymd [2] = "0" + ymd [2];
30 return ymd. Join ("/");
31}
(2)Remove leading and trailing Spaces
1 // left and right spaces, excluding the tab key
2 // parameter: String
3 // return: string with spaces removed
4 function trim (s ){
5 return S. Match (/^ [\ t] * (. *?) [\ T] * $/) = NULL? S: Regexp. $1;
6}
(3)Obtain the byte function.
// Obtain the number of texts
Function getbytecount (value ){
VaR intcount = 0;
For (VAR I = 0; I <value. length; ++ I ){
VaR sub = value. substring (I, I + 1 );
If (chkiszen (sub) | sub = "<" | sub = "'" | sub = ">" | sub = "&"){
Intcount + = 2;
} Else {
Intcount + = 1;
}
}
Return intcount;
}