JS string, array, time, Date object

Source: Internet
Author: User

JS on string, array, date operation is the most frequently used in future projects, so .... So we look at it, in fact, not difficult ha, is a bit boring, I admit that this is my Baidu, haha haha

<! DOCTYPE html>


<meta charset= "UTF-8" >
<title>javascript String Object </title>

<body>
<script type= "Text/javascript" >

1. Manipulation of strings
var s1= "Instantiate string Way One";//The first way to instantiate a string
var s2=new string ("instantiate string ABC mode II");//The second creates a string in new way
document.write (s1+ "<br/>");
document.write (s2+ "<br/>");
document.write (s2+ "length is:" +s2.length+ "<br/>");//Get the length of the string
document.write (S2.indexof ("abc", 0) + "<br/>");//Get the position of ABC starting from 0
document.write (S2.replace ("abc", "How are You?") ") +" <br/> ");//Use Hello? Replace ABC

2. Operation of the date

var date=new date ();//Date Created
document.write (Date.gettime () + "<br/>");//Get the number of milliseconds from 1970 to present
document.write (Date.getfullyear () + "<br/>");//Get the current year
document.write ((Date.getmonth () +1) + "<br/>");//Get the current month
document.write (Date.getdate () + "<br/>");//Get the current day
var today=date.getfullyear () + "year" + (Date.getmonth () +1) + "month" +date.getdate () + "Day";//Note month need to add a
document.write (today+ "<br/>");
document.write (date.gethours () + "<br/>");//Get current time
document.write (date.getminutes () + "<br/>");//Get current points
document.write (Date.getseconds () + "<br/>");//Get current second
Today=date.getfullyear () + "year" + (Date.getmonth () +1) + "month" +date.getdate () + "Day" +date.gethours () + ":" +date.getminutes () + ":" +date.getseconds ();
document.write (today+ "<br/>");
var day=date.getday ();//Get the day of the week (note foreigner is from 0-6)
VAR week;
Switch (day) {
Case 0:week= "Sunday";
Case 1:week= "Monday";
Case 2:week= "Tuesday";
Case 3:week= "Wednesday";
Case 4:week= "Thursday";
Case 5:week= "Friday";
Case 6:week= "Saturday";
}
document.write (week+ "<br/>");
Today=date.getfullyear () + "year" + (Date.getmonth () +1) + "month" +date.getdate () + "Day" +week+ "" +date.gethours () + ":" + Date.getminutes () + ":" +date.getseconds ();
document.write (today+ "<br/>");

JS Operation Array

var arr=new array ();//create an array without a specified length
var arr2=new array (3);//Create an array of the specified length
Arr[0]= "Jack";//Assigning a value to an array element
Arr[1]= "Marry";
arr[2]= "Tom";
Arr[3]= "Lucy";
arr[4]= "June";

arr2[0]=2;
Arr2[1]=1;
arr2[2]=5;

for (Var i=0;i<arr.length;i++) {//First loop array method
document.write (arr[i]+ "<br/>");
}
document.write ("var o;
For (o in arr2) {//for-in loop array method (Note: O represents the subscript of the current array)
document.write (o+ "" +arr2[o]+ "<br/>");
}
document.write ("document.write ("Sort ()" +arr.sort () + "<br/>");
document.write ("Sort ()" +arr2.sort () + "<br/>");
document.write ("document.write ("Join ()" +arr.join () + "<br/>");
document.write ("Join ()" +arr2.join (".") + "<br/>");
document.write ("document.write ("concat ()" +arr.concat (ARR2) + "<br/>");
document.write ("document.write ("reverse ()" +arr.reverse () + "<br/>");

</script>
</body>

JS string, array, time, Date object

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.