Home  >  Article  >  Web Front-end  >  Detailed explanation of JS built-in object instances

Detailed explanation of JS built-in object instances

小云云
小云云Original
2018-03-14 17:23:521496browse

Date date object The date object can store any date and can be accurate to milliseconds (1/1000 seconds).

Define a time object:

var Udate=new Date();
Note: When using the keyword new, the first letter of Date() must be capitalized.

Make Udate a date object, and it has an initial value: current time (current computer system time).

If you want to customize the initial value, you can use the following method:

var d = new Date(2012, 10, 1); //October 1, 2012
var d = new Date('Oct 1, 2012'); //October 1, 2012
We'd better use the "method" introduced below to strictly define the time.

Access method syntax: "d782e30047dc6d5c072e9df3a62a2773.4f4c080df8bae601f1e89959feb25b5a"

Common methods for processing time and date in Date objects:

返回/设置年份方法 get/setFullYear() 返回/设置年份,用四位数表示。
var mydate=new Date();//当前时间2014年3月6日 document.write(mydate+””);
//输出当前时间 document.write(mydate.getFullYear()+””);
//输出当前年份 mydate.setFullYear(81); 
//设置年份 document.write(mydate+””); 
//输出年份被设定为 0081年。

Note: Different browsers have different results of mydate.setFullYear(81). The year is set to 0081 or 81.

Result:

Thu Mar 06 2014 10:57:47 GMT+0800
2014
Thu Mar 06 0081 10:57:47 GMT+0800
Note:

1. The result format is: week, month, day, year, hour, minute, second, time zone. (Firefox)

  1. Different browsers have different time formats.

  2. Returning day of the week method
    getDay() returns the day of the week, returning a number from 0 to 6, 0 means Sunday. If you want to return the corresponding "week", complete it through an array, the code is as follows:

var mydate=new Date();//Define the date object var weekday=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]; //Define the array object and assign a value to each array item var mynum=mydate.getDay();//The return value is stored in the variable mynum document.write(mydate.getDay());//Output getDay() to get the value document.write("Today is: "+ weekday[mynum]);//Output the day of the week

Note: The above code was run on Friday, March 7, 2014.

Result:

5

Today is: Friday
Return/set time method
get/setTime() Return/set time in milliseconds, Calculates the number of milliseconds from 0:00 on January 1, 1970 to the date pointed to by the date object.

If you delay the time of the current date object by 1 hour, the code is as follows:

var mydate=new Date();
  document.write("当前时间:"+mydate+"
");
  mydate.setTime(mydate.getTime() + 60 * 60 * 1000);
  document.write("推迟一小时时间:" + mydate);

Result:

Current time: Thu Mar 6 11:46:27 UTC+0800 2014

Delay one hour: Thu Mar 6 12:46:27 UTC+0800 2014

Note: 1. One hour 60 minutes, one minute 60 seconds, one second 1000 milliseconds

  2. 时间推迟 1 小时,就是: “x.setTime(x.getTime() + 60 * 60 * 1000);”

String String object
We have used string objects in previous studies. The way to define a string is to assign a value directly. For example:

var mystr = "I love JavaScript!"
After defining the mystr string, we can access its properties and methods.

Access the property length of the string object:

stringObject.length; Returns the length of the string.

var mystr=”Hello World!”;
var myl=mystr.length;
After the above code is executed, the value of myl will be: 12

Access string object Method:

Use the toUpperCase() method of the String object to convert the string lowercase letters to uppercase:

var mystr=”Hello world!”;
var mynum=mystr. toUpperCase();
After the above code is executed, the value of mynum is: HELLO WORLD!
Return the character at the specified position
The charAt() method can return the character at the specified position. The characters returned are strings of length 1.

Syntax:

stringObject.charAt(index)
Parameter description:

Note: 1. The subscript of the first character in the string is 0. The subscript of 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, this method will return an empty string.

For example: in the string "I love JavaScript!", return the character at position 2:

<script type="text/javascript">
  var mystr="I love JavaScript!"
  document.write(mystr.charAt(2));</script>

Note: A space is also counted as a character.
Returns the position where the specified string first appears.
The indexOf() method can return the position where a specified string value first appears in the string.

Syntax

stringObject.indexOf(substring, startpos)
Parameter description:
Description:

1. This method will retrieve the string from beginning to end stringObject to see if it contains substring substring.

2. Optional parameter, search for substring starting from the startpos position of stringObject. If there is no such parameter, it will search from the starting position of stringObject.

3. If a substring is found, return the position of the first occurrence of substring. Character positions in stringObject start from 0.

Note: 1. The indexOf() method is case-sensitive.

2. If the string value to be retrieved does not appear, this method returns -1.

For example: Perform different searches within the "I love JavaScript!" string:

var str="I love JavaScript!"
  document.write(str.indexOf("I") + "
");
  document.write(str.indexOf("v") + "
");
  document.write(str.indexOf("v",8));

The output of the above code:

0
4
9

Related recommendations:

js built-in object learning notes_javascript skills

The above is the detailed content of Detailed explanation of JS built-in object instances. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn