Detailed explanation of JavaScript's built-in Date object

Detailed explanation of JavaScript's built-in Date object

Date Object

Use Date object to represent a time in JS

Creating a Date Object

new Date()

Creating a Date object If you use the constructor to create a Date object, it will encapsulate the time when the current code is executed.

var d = new Date();
console.log("Current time is:",d);

insert image description here

Create a specified time object

You need to pass a string representing the time as a parameter in the constructor

Date format month/day/year (hour:minute:second)

var d = new Date("12/21/2012 12:12:30");
console.log("The Mayans predicted the end of the world:",d);

insert image description here

You can also create it by passing parameters

The syntax is

new Date(y,M,d,h,m,s):帶參的構造,參數是年、月、日、時、分、秒

var d = new Date(2012,11,21,8,00,00);
console.log(d);

insert image description here

Notice:

The integer value of the month of the time created by parameter passing, from 0 (January) to 11 (December)

getDate()

Get the date of the current object

var d = new Date("12/21/2012 12:12:30");
var date = d.getDate()
console.log("What is the date of object d:", date);

insert image description here

getDay()

  • Get the day of the week of the current date object
  • Will return a value from 0 to 6
    • 0 means Sunday
    • 1 for Monday
    • 2 for Tuesday
    • 3 for Wednesday
    • .......
var d = new Date("12/21/2012 12:12:30");
var date = d.getDay()
console.log("What day of the week is object d: ", date);

insert image description here

getMonth()

  • Get the month of the current time object
  • It will return a value between 0 and 11 (usually with 1 added to indicate the month commonly used in China)
    • 0 means January
    • 1 means February
    • ........
    • 11 means December
var d = new Date("12/21/2012 12:12:30");
var date = d.getMonth()
console.log("The month of the current time object is:", date); //Returns a number from 0 to 11, 11 represents December 

insert image description here

getFullYear()

  • Get the year of the current date object
  • This method has been replaced
var d = new Date("12/21/2012 12:12:30");
var date = d.getFullYear()
console.log("Year of the current time object:", date);

insert image description here

getHours()

  • Get the hour of the current date object
  • Return value (0~23)

getMinutes()

  • Get the minute of the current date object
  • Return value (0~59)

getSeconds()

  • Get the seconds of the current date object
  • Return value (0~59)

getMilliseconds()

  • Get the milliseconds of the current date object
  • Returns a value (0~999)

getTime()

  • Get the timestamp of the current date and time
  • Timestamp, which is the number of milliseconds from 0:0:0:00 Greenwich Mean Time, January 1, 1970 to the current object date (1 second = 1000 milliseconds)
  • The computer bottom layer uses timestamps to save time.
  • It can be converted to the current object time by (time/1000/60/60/24/365)
var d = new Date("12/21/2012 11:10:30");
var date = d.getTime()
console.log("Year of the current time object:", date);

insert image description here

Date.now()

  • Get the current timestamp
  • Timestamps can be used to test the performance of code execution
var start = Date.now();
for (let i = 0; i < 100; i++)
{
    console.log(i);
}
var end = Date.now();
console.log("The statement was executed: "+(end - start)+" milliseconds");

insert image description here

toDateString()

  • Convert date to string

toLocaleDateString()

  • Convert a date to a string in the local date format

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of JavaScript built-in object operations
  • Summary of JS front-end knowledge points: built-in objects, date objects and timer related operations
  • Analysis of common usage examples of JavaScript reference type Date
  • JavaScript built-in object Date case summary analysis

<<:  Control the vertical center of the text in the HTML text box through CSS

>>:  Solution for converting to inline styles in CSS (css-inline)

Recommend

Let's talk about my understanding and application of React Context

Table of contents Preface First look at React Con...

Example of fork and mutex lock process in Linux multithreading

Table of contents Question: 1. First attempt 2. R...

What are the benefits of semantic HTML structure?

one: 1. Semantic tags are just HTML, there is no ...

A brief analysis of the best way to deal with forgotten MySQL 8 passwords

Preface Readers who are familiar with MySQL may f...

How to call a piece of HTML code together on multiple HTML pages

Method 1: Use script method: Create a common head...

Use iptables and firewalld tools to manage Linux firewall connection rules

Firewall A firewall is a set of rules. When a pac...

One-click installation of MySQL 5.7 and password policy modification method

1. One-click installation of Mysql script [root@u...

Summary of MySQL character sets

Table of contents Character Set Comparison Rules ...

Docker image analysis tool dive principle analysis

Today I recommend such an open source tool for ex...

Summary of tips for setting the maximum number of connections in MySQL

Method 1: Command line modification We only need ...

Exploring the use of percentage values ​​in the background-position property

How background-position affects the display of ba...

Detailed process analysis of docker deployment of snail cinema system

Environmental Statement Host OS: Cetnos7.9 Minimu...

Vue implements weather forecast function

This article shares the specific code of Vue to r...

Alpine Docker image font problem solving operations

1. Run fonts, open the font folder, and find the ...