


Commonly used encapsulation methods for Date objects and solutions to problems encountered
The content this article brings to you is about the commonly used encapsulation methods of Date objects and solutions to problems encountered. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
I have been using the Date object in JS for a long time, but I have never recorded the commonly used encapsulation functions and pitfalls encountered. I will record them when I have time today so that I can use them directly next time. , and remind yourself of those pitfalls you have encountered. If there is something wrong, I hope you can point it out, I will be very grateful.
When converting a date (without hours, minutes and seconds) to a timestamp, the date is converted to a time when connected with '-' (2019-01-01) and '/' (2019/01/01) The results of the poke are different
In order to prevent everyone from seeing too many examples and getting bored, I will come to the conclusion first.
Conclusion:
1) If the dates are connected using '-', when the month and day are both less than 9 and a 0 is added in front, then the time will be converted to a timestamp by default 8 o'clock in the morning of the day
2) If the dates are connected using '-', when the month and day are less than 9 and there is a 0 in front of less than 9, then it will be converted to a timestamp. The time is converted to 8 a.m. of the day by default
3) If the dates are connected using '-', when the month and day are both less than 9 and only one is preceded by a 0, then it will be converted to a timestamp The time will be converted to 12 o'clock in the morning of the day by default, which is 00:00
4) If the dates are connected using '-', when the month and day are both greater than 9, then it will be converted to a timestamp The time will be converted to 8 a.m. of the day by default
5) If the dates are connected using '/', then when converted into timestamps, they will only be converted to 00:00 a.m. of the day
Summary: In When converting dates to timestamps, if the hours and minutes are not set, it is best to use '/' to connect to avoid obtaining different timestamps for the same date when written in different ways
The following is an example to prove the conclusion:
" let time1 = new Date('2019-03-03').getTime(); let time2 = new Date('2019/3/3').getTime(); console.log('获取时间') console.log(time1) console.log(time2) console.log( (time1-time2) / 1000 /60 /60 ) // 8 // 根据本地格式,把Date对象的时间转换为字符串 上午12:00:00也就是 00:00:00 console.log(new Date('2019-03-03').toLocaleString()) // 2019/3/3 上午8:00:00 console.log(new Date('2019-03-12').toLocaleString()) // 2019/3/12 上午8:00:00 console.log(new Date('2019-11-03').toLocaleString()) // 2019/11/3 上午8:00:00 console.log(new Date('2019-3-03').toLocaleString()) // 2019/3/3 上午12:00:00 console.log(new Date('2019-03-3').toLocaleString()) // 2019/3/3 上午12:00:00 console.log(new Date('2019-11-13').toLocaleString()) // 2019/11/13 上午8:00:00 console.log(new Date('2019/03/03').toLocaleString()) // 2019/3/3 上午12:00:00 console.log(new Date('2019/3/3').toLocaleString()) // 2019/3/3 上午12:00:00 console.log(new Date('2019/03/3').toLocaleString()) // 2019/3/3 上午12:00:00 console.log(new Date('2019/3/03').toLocaleString()) // 2019/3/3 上午12:00:00 console.log(new Date('2019/03/12').toLocaleString()) // 2019/3/12 上午12:00:00 console.log(new Date('2019/11/03').toLocaleString()) // 2019/11/3 上午12:00:00 "
Convert date format to timestamp and timestamp to date format
1. 将日期格式转换为时间戳的三种方法 "javascript let dateStr = new Date('2019-3-20 18:59:39:123'); let timestamp1 = dateStr.getTime(); // 1553079579123 let timestamp2 = dateStr.valueOf(); // 1553079579123 let timestamp3 = Date.parse(dateStr); // 1553079579000 " date.getTime()、date.valueOf()会精确到毫秒,而Date.parse(date)只能精确到秒,毫秒用000替代 2. 将时间戳转换为日期格式 "javascript function dateFormat(timestamp) { timestamp = (timestamp + '' ).length > 10 ? timestamp : timestamp * 1000; //判断时间戳为几位,10位时加上毫秒,13为的则不管 let date = new Date(timestamp); let year = date.getFullYear(); let month = date.getMonth() + 1 > 9 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1); // 月份从0开始,0~11, 所以显示时要 +1 let day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate() ; let hour = date.getHours() > 9 ? date.getHours() : '0' + date.getHours() ; let minutes = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes(); let seconds = date.getSeconds() > 9 ? date.getSeconds() : '0' + date.getSeconds(); return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds; } "
Compare how many days are between two dates
/** * @method 计算两个日期之间有几天,包括第一天 * @param beginTime 开始时间的日期 '2019-3-19' || '2019/3/19' * @param endTime 结束时间的日期 '2019-3-20' || '2019/3/19' */ getIntervalDay('2019-03-03', '2019-03-8'); // 若是没有用 正则将格式转换的话得到的结果是5天,转换后是6天 function getIntervalDay(beginTime, endTime) { // 先利用将其转换为统一的格式,因为 '-' 格式下的时间戳转换的结果不一致,原因在本文的开头 beginTime = beginTime.replace(/\-/g, '/'); endTime = endTime.replace(/\-/g, '/'); let time1 = new Date(beginTime).getTime(); let time2 = new Date(endTime).getTime(); // console.log(beginTime) // console.log(endTime) let second = time2 - time1; let day = parseInt(second / (1000 * 60 * 60 * 24)) + 1; // 当天也算进去 return day; }
Judge how many days there are in a year Days
// 闰年为366天(2月中多一天),平年为365天。 // 闰年有两种: 1)普通闰年:能被4整除但不能被100整除的年份为普通闰年。 // 2)世纪闰年:能被400整除的为世纪闰年。 function getYearAllDay(year) { return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0 ? 366 : 365; }
Get the total number of days in a month in a certain year
// date格式为 'xxxx-xx-xx' 'xxxx/xx/xx' 'xxxx/xx' 'xxxx-xx' function getMonthAllDay(date) { date = new Date(date); let year = date.getFullYear(); let month = date.getMonth() + 1; // 从 Date 对象返回月份 (0 ~ 11)。 let nextMonth = year + '-' + (month + 1); let newDate = new Date(nextMonth); newDate.setDate(0); // 利用设置日期时从1~31设置,当设置为0时,即上个月的最后一天 return newDate.getDate(); }This article has ended here. For more other exciting content, you can pay attention to the JavaScript tutorial video# on the PHP Chinese website. ##Column!
The above is the detailed content of Commonly used encapsulation methods for Date objects and solutions to problems encountered. For more information, please follow other related articles on the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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

WebStorm Mac version
Useful JavaScript development tools