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

How to use CSS to achieve two columns fixed in the middle and adaptive

1. Use absolute positioning and margin The princi...

Learning Vue instructions

Table of contents 1. v-text (v-instruction name =...

Common operation commands of MySQL in Linux system

Serve: # chkconfig --list List all system service...

Specific usage of textarea's disabled and readonly attributes

disabled definition and usage The disabled attrib...

How to use native JS to implement touch sliding monitoring events

Preface I wrote a small demo today. There is a pa...

CSS3 to achieve simple white cloud floating background effect

This is a very simple pure CSS3 white cloud float...

How to solve the mysql ERROR 1045 (28000)-- Access denied for user problem

Problem description (the following discussion is ...

Detailed explanation of 8 ways to pass parameters in Vue routing components

When we develop a single-page application, someti...

Vue implements tab label (label exceeds automatic scrolling)

When the created tab label exceeds the visible ar...

MySQL 5.7.18 MSI Installation Graphics Tutorial

This article shares the MySQL 5.7.18 MSI installa...

Is your website suitable for IE8?

During the Olympic Games, IE 8 Beta 2 will be rele...