A wave of JavaScript date judgment script sharing_javascript skills
1. Compare two dates
var x = new Date('2015-05-25'); var y = new Date('2015-05-25'); if (x.getTime() == y.getTime()) { It'll be true; }
2. Whether it is between two dates
var beforeSpringDate, afterSpringDate; for (var i = 0; i < springFestivalDays.length; i++) { beforeSpringDate = new Date(springFestivalDays[i]); beforeSpringDate.setDate(beforeSpringDate.getDate() - 4); afterSpringDate = new Date(springFestivalDays[i]); afterSpringDate.setDate(afterSpringDate.getDate() + springFestivalPeriod - 1); if (time >= beforeSpringDate.getTime() && time <= afterSpringDate.getTime()) { result = true; break; } }
3. One line of code to determine whether it is a leap year
var input = new Date();new Date(input.getFullYear(), 1, 29).getDate() === 29 false var input = new Date(2012,1,1);new Date(input.getFullYear(), 1, 29).getDate() === 29 true var input = new Date(2013,1,1);new Date(input.getFullYear(), 1, 29).getDate() === 29 false var input = new Date(2014,1,1);new Date(input.getFullYear(), 1, 29).getDate() === 29 false var input = new Date(2014,5,1);new Date(input.getFullYear(), 1, 29).getDate() === 29 false var input = new Date(2014,6,1);new Date(input.getFullYear(), 1, 29).getDate() === 29 false var input = new Date(2016,6,1);new Date(input.getFullYear(), 1, 29).getDate() === 29 true var input = new Date(2000,6,1);new Date(input.getFullYear(), 1, 29).getDate() === 29 true
4. Determine the status of the week before and after major holidays (National Day, Spring Festival)
var holiday = {}; function inNationalDay(date) { var result = { beforeNationalDay: false, duringNationalDay: false }; if (date) { var month = date.getMonth() + 1, day = date.getDate(); if (month == 9 && (day >= 24 && day <= 30)) { result.beforeNationalDay = true; } else if(month == 10 && (day >= 1 && day <= 7)) { result.duringNationalDay = true; } } return result; } function inSpringFestival(date) { var result = { beforeSpringFestival: false, duringSpringFestival: false }; if (date) { // set GMT+0800 hours(set china hour +8) date = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 8); var time = date.getTime(); var springFestivalDays = ['2015-02-19', '2016-02-08', '2017-01-28', '2018-02-16', '2019-02-05', '2020-01-25', '2021-02-12', '2022-02-01', '2023-01-22', '2024-02-10']; var springDay, springDate, beforeSpringDate, afterSpringDate; for (var i = 0; i < springFestivalDays.length; i++) { springDay = springFestivalDays[i]; springDate = new Date(springDay); beforeSpringDate = new Date(springDay); beforeSpringDate.setDate(beforeSpringDate.getDate() - 7); afterSpringDate = new Date(springDay); afterSpringDate.setDate(afterSpringDate.getDate() + 7); if (time >= beforeSpringDate.getTime() && time < springDate.getTime()) { result.beforeSpringFestival = true; break; } else if (time >= springDate.getTime() && time < afterSpringDate.getTime()) { result.duringSpringFestival = true; break; } } } return result; } holiday.cache = { lastUpdateDate: null, beforeHoliday: false, duringHoliday: false }; holiday.checkHoliday = function() { var now = new Date(); if (!holiday.cache.lastUpdateDate) { holiday.cache.lastUpdateDate = now; var nationalDayResult = inNationalDay(now); var springFestivalResult = inSpringFestival(now); holiday.cache.beforeHoliday = nationalDayResult.beforeNationalDay || springFestivalResult.beforeSpringFestival; holiday.cache.duringHoliday = nationalDayResult.duringNationalDay || springFestivalResult.duringSpringFestival; } else { var lastUpdateDate = holiday.cache.lastUpdateDate; var cacheDate = lastUpdateDate.getFullYear() + '' + lastUpdateDate.getMonth() + '' + lastUpdateDate.getDate(); var nowDate = now.getFullYear() + '' + now.getMonth() + '' + now.getDate(); if (cacheDate != nowDate) { holiday.cache.lastUpdateDate = now; var nationalDayResult = inNationalDay(now); var springFestivalResult = inSpringFestival(now); holiday.cache.beforeHoliday = nationalDayResult.beforeNationalDay || springFestivalResult.beforeSpringFestival; holiday.cache.duringHoliday = nationalDayResult.duringNationalDay || springFestivalResult.duringSpringFestival; } } }; module.exports = holiday;
5. Determine the day of the week
<SCRIPT> var s = '2011-11-17'; alert( "今天星期 "+"天一二三四五六 ".charAt(new Date(s).getDay())); alert("星期 " + new Date(s).getDay()); </SCRIPT>
You can also do this:
var weekDay = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]; var dateStr = "2008-08-08"; var myDate = new Date(Date.parse(dateStr.replace(/-/g, "/"))); alert(weekDay[myDate.getDay()]);

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

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.


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

SublimeText3 Chinese version
Chinese version, very easy to use

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

SublimeText3 Linux new version
SublimeText3 Linux latest version

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.

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