search
HomeWeb Front-endJS TutorialDetailed explanation of the properties and methods of the Date object of JavaScript native objects_Basic knowledge

Syntax for creating Date objects:

Copy code The code is as follows:

//The Date object will automatically save the current date and time to its initial values.
new Date();

//value-milliseconds: represents the value starting from 00:00:00 on January 1, 1970 UTC.
new Date(value);

//dateString-date string: a string value representing a date. This string should be in the format recognized in the parse method.
new Date(dateString);

//year-year: an integer value representing the year. To avoid year 2000 problems it is best to specify a 4-digit year; use 1998, not 98
//month-month: an integer value representing the month from 0 (January) to 11 (December)
//day-day: an integer value representing the day of the month, starting from 1
//hour-hour: an integer value representing the number of hours in a day (24-hour format)
//minute-minute
//second-second
//millisecond-millisecond
new Date(year, month, day [, hour, minute, second, millisecond]);

Date()

Date() method returns today’s date and time.

Copy code The code is as follows:

console.log(Date()); //"Tue Sep 17 2013 12:22:55 GMT 0800 (China Standard Time)"

parse()

The parse() method parses a datetime string and returns the number of milliseconds from midnight on January 1, 1970 to the datetime.

Date.parse(datestring)

The parameter datestring is required and represents a string of date and time.

Note that this method is a static method of the Date object. This method is generally called in the form of Date.parse() rather than through dateobject.parse().

Copy code The code is as follows:

console.log(Date.parse(Date())); //1379392301000
console.log(Date.parse("Aug 9, 1995")); //807897600000

UTC()

UTC() method returns the number of milliseconds from January 1, 1970 to the specified date according to universal time.

Date.UTC(year, month, day, hours, minutes, seconds, ms)

The parameter year is required, representing the four-digit number of the year; month is required, representing the integer of the month, ranging from 0 ~ 11; day is optional, representing the integer of the date, ranging from 1 ~ 31; hours is optional, representing an integer of hours, ranging from 0 ~ 23; minutes is optional, representing an integer of minutes, ranging from 0 ~ 59; seconds is optional, representing an integer of seconds, ranging from 0 ~ 59; ms is optional and represents an integer in milliseconds, ranging from 0 to 999.

Date.UTC() is a static method. The parameters of the Date.UTC() method specify the date and time, which are both UTC times, in the GMT time zone. The specified UTC time is converted to milliseconds so that it can be used by the Date() constructor and the Date.setTime() method.

The Date type in ECMAScript is built on the Java.util.Date class in early Java. For this purpose, the Date type uses the number of milliseconds that have elapsed since midnight (zero hour) on January 1, 1970, UTC (Coordinated Universal Time, International Coordinated Time) to save the date. Under the conditions of using this data storage format, the date saved by the Date type can be accurate to 285616 years before or after January 1, 1970.

Note: parse() dates and times are created based on the local time zone, not GMT. UTC() is created based on GMT. Their parameters are also different.

GMT: Universal Time, which is the standard time in Greenwich.

Copy code The code is as follows:

var d = new Date();

console.log(Date.parse(d)); //1379393562000
console.log(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds()) ); //1379422362020

ECMAScript5 adds the Data.now() method, which returns the number of milliseconds representing the date and time when this method is called. IE9 only started to support it, but we can use the operator to convert the Data object into a string and get the same value.

Copy code The code is as follows:

var d1 = Date.now();
var d2 = new Date();

console.log(d1); //1379393793104
console.log(d2); //1379393793104

Date conversion in JavaScript is very weird. Not only will there be different interpretation results due to different parameters, but the performance in each browser is also different, as follows:

Copy code The code is as follows:

var d1 = new Date("2012/03/13");
var d2 = new Date("2012-03-13");
var d3 = new Date("2012-3-13");

console.log(d1); //Tue Mar 13 2012 00:00:00 GMT 0800 (China Standard Time)
console.log(d2); //Tue Mar 13 2012 08:00:00 GMT 0800 (China Standard Time)
console.log(d3); //Tue Mar 13 2012 00:00:00 GMT 0800 (China Standard Time)

Performance reference in different browsers: http://dygraphs.com/date-formats.html

To avoid these problems, follow the advice below:

1. Stick to the date string format of “YYYY/MM/DD”
2. Avoid using the date string format "YYYY-MM-DD" with hyphens
3. Specify the four-digit year
4. Chrome browser can accept more date strings than other browsers, so if there is no problem in Chrome browser, it does not mean that there is no problem in other browsers

For more information, please refer to: JavaScript and Dates, What a Mess! and Discussions in SO

get series method

getDate() returns the day of the month (1 ~ 31) from the Date object.
getDay() returns the day of the week (0 ~ 6) from a Date object.
getMonth() returns the month (0 ~ 11) from a Date object.
getFullYear() Returns the year as a four-digit number from a Date object. Be careful not to use getYear().
getHours() returns the hours (0 ~ 23) of a Date object.
getMinutes() returns the minutes (0 ~ 59) of a Date object.
getSeconds() returns the number of seconds in a Date object (0 ~ 59).
getMilliseconds() returns the milliseconds (0 ~ 999) of the Date object.
getTime() returns the number of milliseconds since January 1, 1970.
getTimezoneOffset() Returns the difference in minutes between local time and Greenwich Mean Time (GMT).
getUTCDate() Returns the day of the month (1 ~ 31) from a Date object based on universal time.
getUTCDay() Returns the day of the week (0 ~ 6) from a Date object based on universal time.
getUTCMonth() returns the month (0 ~ 11) from a Date object based on universal time.
getUTCFulYear() Returns the four-digit year from a Date object based on universal time.
getUTCHours() returns the hour (0 ~ 23) of a Date object according to universal time.
getUTCMinutes() returns the minutes (0 ~ 59) of a Date object according to universal time.
getUTCSeconds() Returns the seconds (0 ~ 59) of a Date object according to universal time.
getUTCMilliseconds() returns the milliseconds (0 ~ 999) of a Date object according to universal time.

set series methods

setDate() sets the day of the month (1 ~ 31) in the Date object.
setMonth() sets the month (0 ~ 11) in the Date object.
setFullYear() Sets the year (four digits) in a Date object. Be careful not to use the setYear() method.
setHours() sets the hours (0 ~ 23) in the Date object.
setMinutes() sets the minutes (0 ~ 59) in the Date object.
setSeconds() sets the seconds (0 ~ 59) in the Date object.
setMilliseconds() sets the milliseconds (0 ~ 999) in the Date object.
setTime() sets a Date object in milliseconds.
setUTCDate() Sets the day of the month (1 ~ 31) in the Date object according to universal time.
setUTCMonth() Sets the month (0 ~ 11) in the Date object according to universal time.
setUTCFulYear() Sets the year (four digits) in a Date object according to universal time.
setUTCHours() Sets the hour (0 ~ 23) in a Date object according to universal time.
setUTCMinutes() Sets the minutes in a Date object (0 ~ 59) according to universal time.
setUTCSeconds() Sets the seconds in a Date object (0 ~ 59) according to universal time.
setUTCMilliseconds() Sets the milliseconds in a Date object (0 ~ 999) according to universal time.

toString series methods

toString() Converts a Date object to a string. toString() always returns a string expressed in American English.
toTimeString() Converts the time portion of a Date object to a string.
toDateString() Converts the date portion of a Date object to a string.
toUTCString() Converts a Date object to a string according to universal time.
toLocaleString() Converts a Date object to a string according to the local time format.
toLocaleTimeString() Converts the time part of the Date object to a string according to the local time format.
toLocaleDateString() Converts the date part of the Date object to a string according to the local time format.

Copy code The code is as follows:

var d = new Date();

console.log(d);                                                       //Tue Sep 17 2013 13:37:04 GMT 0800 (China Standard Time)
console.log(d.toString()); //Tue Sep 17 2013 13:37:04 GMT 0800 (China Standard Time)
console.log(d.toTimeString()); //13:37:04 GMT 0800 (China Standard Time)
console.log(d.toDateString() ); //Tue Sep 17 2013
console.log(d.toUTCString()); //Tue, 17 Sep 2013 05:37:04 GMT
console.log(d.toLocaleString()); //September 17, 2013 1:37:04 pm
console.log(d.toLocaleTimeString()); //1:37:04 PM
console.log(d.toLocaleDateString()); //September 17, 2013

Note that the toLocaleString() series of methods can receive parameters to determine the output convention. Reference: MDN

Copy code The code is as follows:

var d = new Date();

console.log(d.toLocaleString("ko-KR")); //2013년 9월 17일 오후 1:48:24

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
The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function