Detailed introduction to Date object in JavaScript (code example)
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 (中国标准时间)
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
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
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 (中国标准时间)
Time stamp format conversion
dateFormmat(time) { let date = new Date(time * 1000); //如果date为13位不需要乘1000 let Ye = date.getFullYear() + '/'; let Mo = (date.getMonth() + 1 <p><strong>Date instance-(get)</strong></p><p>All Date instances inherit from Date .prototype. Modifying the prototype object of the Date constructor affects all Date instances. </p><p><strong>Date.getDate()</strong></p><p>Returns a specified date object as the day of the month based on local time. getDate() returns an integer value from 1 to 31</p><pre class="brush:php;toolbar:false">let date = new Date("December 25, 2019 11:11:00"); let day = date.getDate(); console.log(day) //25
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!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

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),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1
Easy-to-use and free code editor
