In daily life, various forms of time characters are everywhere. The emergence of the concept of time, the invention of time units and timing tools have brought about changes to human beings that are difficult to describe in just one word. Today let’s talk about dates. Let’s take a look at the Date object in JavaScript.
Date object is one of the more commonly used objects, but many people do not know how to operate it at all. Even for some simple operations, they use moment without trying it themselves.
This time I will share the date usage skills in Date, hoping to inspire everyone.
MDN official website introduction
The 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 will be set to the last day of the previous month.
Get the number of days in the month
// 获取月份天数 function getMonthDayCount(year, month) { return new Date(year, month, 0).getDate(); } console.log(getMonthDayCount(2017, 10)); // 31
Date The essence of the third parameter is the same as setDate.
Because when date is 0, it automatically returns to the last day of the previous month, so there is no need to decrease the month here, which is just right.
Get the number of days in all months
function getAllMonthDayCount(year) { var days = [31, new Date(year, 2, 0).getDate(), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; return days; } console.log(getAllMonthDayCount(2016));// [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
This is an extension of the above, not much explanation.
Is it a leap year?
function isLeapYear(year) { return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0); }
This is the code on the Internet, I believe most people use it.
But do you really understand or remember it?
I can’t anyway. .
function isLeapYear(year) { return new Date(year, 2, 0).getDate() === 29; } console.log([ isLeapYear(2000), isLeapYear(2016), isLeapYear(2017), isLeapYear(2018) ]); // [ true, true, false, false ]
Looking at it this way, it is very simple and easy to understand.
And you don’t need to remember it. Do you think you can’t forget it even if you want to?
Addition and subtraction of days
I have seen someone use relative seconds to calculate days before or after days, or even across months and years. Condition.
In fact, just setDate directly, and it will automatically handle the situation of crossing months and years.
// 10天后是几月几号 var dt = new Date('2016-12-25'); dt.setDate(dt.getDate() + 10); console.log(dt.toLocaleDateString()); // 2017/1/4 // 10天前是几月几号 var dt = new Date('2017-01-04'); dt.setDate(dt.getDate() - 10); console.log(dt.toLocaleDateString()); // 2016/12/25
Summary
Although these things are very basic, everyone may know the name of the method, but many people still don’t Will use it.
It’s the same as using jq but still using a for loop to process the results.
The above is the detailed content of Summary of Date objects in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

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

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.


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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

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

Atom editor mac version download
The most popular open source editor
