Table of Contents
Date.getDay()
Date.getFullYear()
Date.getMonth()
Date.getHours()
Date.getMinutes()
Date.getSeconds()
Date.getMilliseconds()
Date.getTime()
Date instance-(set)
Date.setDate()
Date.setFullYear()
Date.setHours()
Date.setMilliseconds()
Date.setMinutes()
Date.setMonth()
Date.setSeconds()
Date.setTime()
Home Web Front-end JS Tutorial Detailed introduction to Date object in JavaScript (code example)

Detailed introduction to Date object in JavaScript (code example)

Jan 07, 2019 am 10:05 AM
date javascript

This article brings you a detailed introduction (code example) about the Date object in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Date instances are used to handle dates and times. The Date object is based on the number of milliseconds since January 1, 1970 (UTC).

JavaScript's Date object provides several UTC time methods, and also provides local time methods accordingly. UTC, which is what we call Greenwich Time, refers to the world time standard in time. The local time refers to the time set by the client computer executing JavaScript.

Date constructor

new Date();
//Sun Jan 06 2019 20:18:04 GMT+0800 (中国标准时间)

new Date(value); 
//value 代表自1970年1月1日00:00:00 (世界标准时间) 起经过的毫秒数。
new Date(000000000000);

//Thu Jan 01 1970 08:00:00 GMT+0800 (中国标准时间)

new Date(dateString);
//dateString表示日期的字符串值。该字符串应该能被 Date.parse() 方法识别
new Date("2019.01.01");
//Tue Jan 01 2019 00:00:00 GMT+0800 (中国标准时间)

new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);

//year代表年份的整数值。为了避免2000年问题最好指定4位数的年份; 使用 1998, 而不要用 98.
//month代表月份的整数值从0(1月)到11(12月)。
//day代表一个月中的第几天的整数值,从1开始。
//hour代表一天中的小时数的整数值 (24小时制)。
// minute分钟数。
// second秒数。
//millisecond表示时间的毫秒部分的整数值。

new Date(2019,01,01,01,01,01);

//Fri Feb 01 2019 01:01:01 GMT+0800 (中国标准时间)
Copy after login

Date method

Date.now()

Returns since 1970 -1-1 00:00:00 UTC (Universal Standard Time) The number of milliseconds that have elapsed so far, type is Number.

Date.now()
//1546777708417
Copy after login

Date.parse()

Parse a string representing the date and return the number of milliseconds elapsed from 1970-1-1 00:00:00 .

Date.parse("2019.01.01")
//1546272000000

Date.parse('01 Jan 1970 00:00:00 GMT');
//0
Copy after login

Date.UTC()

Accepts the same arguments as the longest form of the constructor (from 2 to 7), and returns values ​​from 1970-01- The number of milliseconds since 01 00:00:00 UTC.

  • year: A certain year after 1900.

  • month: An integer between 0 and 11, representing the month.

  • date: An integer between 1 and 31, indicating the day of the month.

  • hrs: An integer between 0 and 23 representing hours.

  • min: An integer between 0 and 59, representing minutes.

sec An integer between 0 and 59 representing seconds.
ms
An integer between 0 and 999, representing milliseconds.

new Date(Date.UTC(2019, 0, 0, 0, 0, 1));
//Mon Dec 31 2018 08:00:01 GMT+0800 (中国标准时间)
Copy after login

Time stamp format conversion

  dateFormmat(time) {
    let date = new Date(time * 1000); //如果date为13位不需要乘1000
    let Ye = date.getFullYear() + '/';
    let Mo =
      (date.getMonth() + 1 < 10
        ? '0' + (date.getMonth() + 1)
        : date.getMonth() + 1) + '/';
    let Da =
      (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
    let hh =
      (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    let mm =
      (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
      ':';
    let ss =
      date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    return Ye + Mo + Da +hh + mm + ss
  }
  
//let value=dateFormmat(1234567890)
//console.log(value)
//2009/02/14 07:31:30
Copy after login

Date instance-(get)

All Date instances inherit from Date .prototype. Modifying the prototype object of the Date constructor affects all Date instances.

Date.getDate()

Returns a specified date object as the day of the month based on local time. getDate() returns an integer value from 1 to 31

let date = new Date("December 25, 2019 11:11:00");
let day = date.getDate();

console.log(day)
//25
Copy after login

Date.getDay()

getDay() returns an integer value: 0 represents Sunday, 1 represents Monday, 2 represents Tuesday, By analogy

Date.getFullYear()

getFullYear() method returns the year of the specified date based on local time.

Date.getMonth()

Returns the month of a specified date object based on local time, as a 0-based value (0 represents the first month of the year).

Date.getHours()

The getHours() method returns the hour of a specified date object based on local time. getHours returns an integer value between 0 and 23.

Date.getMinutes()

The getMinutes() method returns the minutes of a specified date object based on local time. getMinutes returns an integer value from 0 to 59

Date.getSeconds()

getSeconds() method returns the seconds of a specified date object based on local time, and returns an integer value from 0 to 59 Integer value.

Date.getMilliseconds()

getMilliseconds() method returns the number of milliseconds in a specified date object based on local time. The getMilliseconds() method returns an integer from 0 to 999.

Date.getTime()

The return value of the getTime method is a numerical value, indicating the distance from the date at 0:00:00 on January 1, 1970 (UTC, that is, Coordinated Universal Time) The number of milliseconds in time represented by the object.

Date instance-(set)

Date.setDate()

setDate() method specifies the number of days in a date object based on local time.
If dayValue is outside the reasonable range of the month, setDate will update the Date object accordingly. For example, if you specify 0 for dayValue, the date is set to the last day of the previous month.

Date.setFullYear()

The setFullYear() method sets the year for a date object based on local time
If one parameter exceeds a reasonable range, the setFullYear method will update other parameter values , the date value of the date object will also be updated accordingly. For example, specifying 15 for monthValue will increase the year by 1 and the month value will be 3.

Date.setHours()

The setHours() method sets the hours for a date object based on local time and returns the updated date from 1970-01-01 00:00:00 UTC The number of milliseconds in time represented by the object instance.

If a parameter exceeds the reasonable range, setHours will update the date information in the date object accordingly. For example, if 100 is specified for secondsValue, the minutes are incremented by 1, and then 40 is used for seconds.

Date.setMilliseconds()

The setMilliseconds() method sets the milliseconds of a date object based on local time.

If the specified number is outside a reasonable range, the time information of the date object will be updated accordingly. For example, if 1005 is specified, the number of seconds is increased by 1 and the number of milliseconds is 5.

Date.setMinutes()

The setMinutes() method sets the minutes for a date object based on local time.

If a specified parameter exceeds a reasonable range, setMinutes will update the time information in the date object accordingly. For example, specifying 100 for secondsValue will increase the number of minutes by 1 and the number of seconds will be 40.

Date.setMonth()

The setMonth() method sets the month for a date object with a set year based on local time

If a specified parameter exceeds a reasonable range, setMonth will respond accordingly Update the date information in the date object. For example, specifying 15 for monthValue increases the year by 1 and uses 3 for the month.

Date.setSeconds()

The setSeconds() method sets the seconds of a date object based on local time.

If a parameter exceeds a reasonable range, the setSeconds method will update the time information of the date object accordingly. For example, specifying 100 for secondsValue will increase the date object's minutes by 1 and use 40 for seconds.

Date.setTime()

The setTime() method sets the time for a Date object with a number of milliseconds representing the time since 1970-1-1 00:00:00 UTC.

https://developer.mozilla.org...

The above is the detailed content of Detailed introduction to Date object in JavaScript (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

See all articles