Common JS user-defined functions (1)

Source: Internet
Author: User

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;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.