Basic syntax summary for JavaScript built-in objects (date date +string string)

Source: Internet
Author: User
Tags time and date time in milliseconds

Tag: The write Time () parameter contains the value of the system Oppo split number

1. A date Date object can store any of the dates and can be accurate to milliseconds (1/1000 seconds).

1) Define a Time object:

var udate=New// Note: Use the first letter of the keyword New,date () to capitalize. Makes udate a Date object and has an initial value: The current time (current computer system time). 

2) If you want to customize the initial value, you can use the following two ways:

var New Date (1);  // October 1, 2012 var New Date ('Oct 1,'//October 1, 2012

3) Common ways to process time and date in adate object:

4) get/setFullYear() Returns/sets the year, expressed in four digits

varMydate=NewDate ();
//Current Time March 6, 2014document.write (mydate+"<br>");
//Output Current Timedocument.write (mydate.getfullyear ()+"<br>");
//Output Current yearMydate.setfullyear (Bayi);
//Set Yeardocument.write (mydate+"<br>");
//The output year is set to 0081 years. Note: Different browsers, Mydate.setfullyear (81) have different results, and the year is set to 0081 or 812 cases. //Results:Thu Mar . the Ten: $: -gmt+0800
//Output Current Time2014
//Output Current yearThu Mar . 0081 Ten: $: -gmt+0800
//set the year and enter the time after Setup//Note: The output current time is in the following format: week, month, day, year, hour, minute, second, time zone.

5)GetDay () returns a number of 0-6, 0 for Sunday. If you want to return the "Week" in the corresponding array, the following example

//It can be done by an array.<script type="Text/javascript">
varMydate=NewDate ();
//Defining Date Objects
varweekday=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
//define array objects, assign values to each array item
varMynum=mydate.getday ();
//Gets the returned number (value) andThe return value is stored in the variable Mynum
document.write (Mydate.getday ());
5
//output getday () Gets the returned number (value)
document.write ("today is:"+ Weekday[mynum]);
Today is: Friday
//get the subscript for the value by Getday (), output the day of the week

</script>

6)get/settime () Returns/sets the time in milliseconds, counting the number of milliseconds from January 1, 1970 0 o'clock to the date the Date object refers to.

//if the time of the current date object is postponed by 1 hours, the unit is in milliseconds<script type="Text/javascript">varMydate=NewDate (); document.write ("Current Time:"+mydate+"<br>"); //Current time: Thu Mar 6 11:46:27 utc+0800Mydate.settime (Mydate.gettime ()+ -* -* +); //one hour, 60 minutes, 60 seconds, 1000 milliseconds per second (in milliseconds)document.write ("To postpone an hour of time:"+mydate); //one hour delay: Thu Mar 6 12:46:27 utc+0800</script>//The time delay is 1 hours, that is: "X.settime (X.gettime () + * *);"

2. String Object

1) A few common uses:

varMystr="Hello world!"; //the way to define a string is to assign a value directly. varMyl=mystr.length;//accesses the String object's property length, returning the stringvarmynum=mystr.touppercase ();//the value of Mynum is: HELLO world!//the toUpperCase () method to convert the string lowercase letters to uppercasevarmynum=mystr.tolowercase ();//the value of Mynum is: Hello world!//the toLowerCase () method to convert the string uppercase letters to lowercase

2)charAt () method to return the character at the specified position. The returned character is a string of length 1.

Note: 1. The subscript for the first character in a string is 0. The subscript for the last character is the string length minus one (string.length-1).

2. If the parameter index is not between 0 and String.length-1, the method returns an empty string.

  <script type="text/javascript">  var mystr="I Love" javascript! "
document.write (Mystr.charat (2// return subscript 2 character L
document.write (Mystr.charat (mystr.length-1// return last character!) ,</script>

3) theindexOf () method returns the position of the first occurrence of a specified string value in a string.

// Grammar stringobject.indexof (substring, startpos)

Note: 1. This method retrieves the string stringobject from beginning to end to see if it contains substring substring.

   2. Optional parameters, starting from the startpos position of the stringobject to find substring, if no this parameter will be found from the beginning of the Stringobject.
   3. If a substring is found, the position of the first occurrence of the substring is returned. The character position in Stringobject is starting at 0.
   4. If the string value that you want to retrieve does not appear, the method returns-1.
Example
<script type="Text/javascript">varStr="I Love javascript!"document.write (Str.indexof ("I") +"<br/>");//0document.write (Str.indexof ("v") +"<br/>");//4document.write (Str.indexof ("v",8));//9</script>

4) Thesplit () method splits the string into an array of strings and returns this array.

// Grammar Stringobject.split (Separator,limit)
// Note: If you use an empty string ("") as a separator, then each character in the Stringobject will be split.
//Example<script type="Text/javascript">varMystr="86-010-85468578";d ocument.write (Mystr.split ('-')+"<br/>");//separates string objects with the character "-" MyStrdocument.write (Mystr.split ("')+"<br/>");//split the String object mystr each character, note: The character split with "leave Blank"document.write (Mystr.split ("',3));//Divides the string object mystr into characters and splits it 3 times. </script>

5) thesubstring () method is used to extract the character of a string intermediary between two specified subscripts

 //  syntax   Stringobject.substring (startpos,stoppos)  1. The returned content is from start (the character containing the start position) to All characters at the stop-1, whose length is stop minus start.  2 . If the argument start is equal to stop, the method returns an empty string (that is, a string of length 0   3 . If start is larger than stop, the method swaps both parameters before extracting the substring. 
// example <script type="text/javascript">  var mystr=" I Love JavaScript " ;  document.write (mystr.substring (7));     // JavaScript   document.write (mystr.substring (2,6));   // Love </script>

6) Thesubstr () method extracts the specified number of strings from the string starting at the startpos position.

// Grammar Stringobject.substr (startpos,length)
// Note: If the parameter startpos is a negative number, the position is calculated from the end of the string. That is,-1 refers to the last character in the string, 2 refers to the second-lowest character, and so on. // If the startpos is negative and the absolute value is greater than the string length, Startpos is 0.
 //  example  <script type="  text/javascript   > var  mystr= " i love javascript!   "  ;  document.write (Mystr.substr ( 7   // javascript!   document.write (Mystr.substr ( 2 , 4   // love 
//2 represents subscript 2, which is: l;
4 means that the subscript is calculated from the last 2 subscript, that is, L will extrapolate 4 characters,
</script>

Basic syntax summary for JavaScript built-in objects (date date +string string)

Related Article

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.