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

The difference and execution method of select count() and select count(1)

Count(*) or Count(1) or Count([column]) are perha...

How to add links to FLASH in HTML and make it compatible with all major browsers

Look at the code first Copy code The code is as fo...

How to use the Linux seq command

1. Command Introduction The seq (Sequence) comman...

Explore the truth behind the reload process in Nginx

Today's article mainly introduces the reload ...

Two implementation solutions for vuex data persistence

Table of contents Business requirements: Solution...

VMware Workstation download and installation detailed tutorial

Virtual machines are very convenient testing soft...

Analysis of Alibaba Cloud CentOS7 server nginx configuration and FAQs

Preface: This article refers to jackyzm's blo...

Docker View Process, Memory, and Cup Consumption

Docker view process, memory, cup consumption Star...

Install MySQL 5.7.17 in win10 system

Operating system win10 MySQL is the 64-bit zip de...

How to adjust the log level of nginx in Docker

Table of contents Intro Nginx Dockerfile New conf...

Clean XHTML syntax

Writing XHTML demands a clean HTML syntax. Writing...

Vue implements partial refresh of the page (router-view page refresh)

Using provide+inject combination in Vue First you...

Learn the common methods and techniques in JS arrays and become a master

Table of contents splice() Method join() Method r...