Detailed explanation of JS built-in object instances
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: "
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)
Different browsers have different time formats.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

Detailed explanation of Linux's curl command Summary: curl is a powerful command line tool used for data communication with the server. This article will introduce the basic usage of the curl command and provide actual code examples to help readers better understand and apply the command. 1. What is curl? curl is a command line tool used to send and receive various network requests. It supports multiple protocols, such as HTTP, FTP, TELNET, etc., and provides rich functions, such as file upload, file download, data transmission, proxy

Detailed explanation of Promise.resolve() requires specific code examples. Promise is a mechanism in JavaScript for handling asynchronous operations. In actual development, it is often necessary to handle some asynchronous tasks that need to be executed in sequence, and the Promise.resolve() method is used to return a Promise object that has been fulfilled. Promise.resolve() is a static method of the Promise class, which accepts a

As a programming language widely used in the field of software development, C language is the first choice for many beginner programmers. Learning C language can not only help us establish the basic knowledge of programming, but also improve our problem-solving and thinking abilities. This article will introduce in detail a C language learning roadmap to help beginners better plan their learning process. 1. Learn basic grammar Before starting to learn C language, we first need to understand the basic grammar rules of C language. This includes variables and data types, operators, control statements (such as if statements,

Golang is a powerful and efficient programming language that can be used to develop various applications and services. In Golang, pointers are a very important concept, which can help us operate data more flexibly and efficiently. Pointer conversion refers to the process of pointer operations between different types. This article will use specific examples to learn the best practices of pointer conversion in Golang. 1. Basic concepts In Golang, each variable has an address, and the address is the location of the variable in memory.
