Javascript date object Date extension method_time and date
Today I excerpted some related methods of operating dates in js from the Internet, and now I will share them with you.
> ;
The specific extension methods are as follows:
parseCHS - static method. Parse commonly used Chinese dates and return date objects.
add - date addition and subtraction operations. [Note: There is a BUG in this function when uploading. Please download and change the first line "var regExp = /^d $/;" in this function to "var regExp = /^([ -])?d $/;", otherwise the subtraction will not be possible. ]
dateDiff - date difference. The difference between the start date and the current date, returns the absolute value of the difference.
getFirstWeekDays——Get the number of days in the first week of the year where the current date is located.
getLastWeekDays——Get the number of days in the last week of the year where the current date is located.
getWeeksOfYear——Get the number of weeks in the year of the current date.
getWeek - Get the week of the year where the current date is. Returns an integer value.
getSeason——Get the season of the year where the current date is. Returns a quarter integer value.
For detailed comments and parameters, please refer to the comments in the JS file.
/*
========================================== ==============================================
Description :Date object extension. Including commonly used Chinese date format analysis, addition and subtraction operations, date difference, weekly operations and quarterly operations.
Author:Dezwen.
Date:2009-5-30.
============================== ================================================== ======
*/
Date.parseCHS = function(dateString) {
///
///Parse commonly used Chinese dates and return date objects.
///
///
//Date string. The formats included are: "xxxx(xx)-xx-xx xx:xx:xx", "xxxx(xx).xx.xx xx:xx:xx",
///"xxxx(xx)年xx" Month xx day xx hour xx minute xx second"
///
var regExp1 = /^d{4}-d{1,2}-d{1,2}( d{ 1,2}:d{1,2}:d{1,2})?$/;
var regExp2 = /^d{4}.d{1,2}.d{1,2}( d{1,2}:d{1,2}:d{1,2})?$/;
var regExp3 = /^d{4} year d{1,2} month d{1,2 }Day (d{1,2} hour d{1,2} minute d{1,2} second)?$/;
if (regExp1.test(dateString)) { }
else if (regExp2 .test(dateString)) {
dateString = dateString.replace(/./g, "-");
}
else if (regExp3.test(dateString)) {
dateString = dateString .replace("year", "-").replace(
"month", "-").replace("day", "").replace("hour", ":").replace(" Minutes", ":"
).replace("seconds", "");
}
else {
throw "The format of the parameter value passed to Date.parseCHS is incorrect. Please pass it. A valid date format string as parameter ";
}
var date_time = dateString.split(" ");
var date_part = date_time[0].split("-");
var time_part = (date_time.length > 1 ? date_time[1].split(":") : "");
if (time_part == "") {
return new Date(date_part[0 ], date_part[1] - 1, date_part[2]);
}
else {
return new Date(date_part[0], date_part[1] - 1, date_part[2], time_part[ 0], time_part[1], time_part[2]);
}
}
Date.prototype.add = function(datepart, number, returnNewObjec) {
///
///Date addition and subtraction.
///If the returnNewObjec parameter is true, the operation result is returned by a new date object, and the original date object remains unchanged.
///Otherwise, the original date object is returned. At this time, the original date object is The value is the result of the operation.
///
///
///The plus and minus parts of the date:
///Year, yy, yyyy--year
///quarter, qq, q--quarter
///Month, mm, m -- month
///dayofyear, dy , y-- day
///Day, dd, d -- day
///Week, wk, ww -- week
///Hour, hh -- hour
// /minute, mi, n -- minutes
///second, ss, s -- seconds
///millisecond, ms -- milliseconds
///
/ //
///Amount to be added or subtracted
///
///
///Whether to return a new date object. If the parameter is true, a new date object is returned, otherwise the current date object is returned.
///
///
///Return a date object
///
var regExp = /^d $/;
if (regExp.test(number)) {
number = parseInt(number);
}
else { number = 0; }
datepart = datepart.toLowerCase();
var tDate;
if (typeof (returnNewObjec) == "boolean" ) {
if (returnNewObjec == true) {
tDate = new Date(this);
}
else { tDate = this; }
}
else { tDate = this ; }
switch (datepart) {
case "year":
case "yy":
case "yyyy":
tDate.setFullYear(this.getFullYear() number );
break;
case "quarter":
case "qq":
case "q":
tDate.setMonth(this.getMonth() (number * 3));
break;
case "month":
case "mm":
case "m":
tDate.setMonth(this.getMonth() number);
break;
case "dayofyear":
case "dy":
case "y":
case "day":
case "dd":
case "d":
tDate.setDate(this.getDate() number);
break;
case "week":
case "wk":
case "ww":
tDate.setDate(this. getDate() (number * 7));
break;
case "hour":
case "hh":
tDate.setHours(this.getHours() number);
break
case "minute":
case "mi":
case "n":
tDate.setMinutes(this.getMinutes() number);
break
case "second" :
case "ss":
case "s":
tDate.setSeconds(this.getSeconds() number);
break;
case "millisecond":
case " ms":
tDate.setMilliseconds(this.getMilliseconds() number);
break;
}
return tDate;
}
Date.prototype.dateDiff = function(datepart, beginDate) {
///
///The difference between the start date and the current date, returns the absolute value of the difference.
///
///
///Additional and subtractive parts of the date:
/// Year, yy, yyyy--year;
///quarter, qq, q --quarter
///Month, mm, m -- month
///dayofyear, dy, y-- Day
///Day, dd, d -- day
///Week, wk, ww -- week
///Hour, hh -- hour
///minute, mi , n -- minutes
///second, ss, s -- seconds
///millisecond, ms -- milliseconds
///
///
///To compare my dates
///
///
///Returns the absolute value of the date difference.
///
datepart = datepart.toLowerCase();
var yearDiff = Math.abs(this.getFullYear() - beginDate.getFullYear());
switch ( datepart) {
case "year":
case "yy":
case "yyyy":
return yearDiff;
case "quarter":
case "qq":
case "q":
var qDiff = 0;
switch (yearDiff) {
case 0:
qDiff = Math.abs(this.getSeason() - beginDate.getSeason()) ;
break;
case 1:
qDiff = (this.getSeason() - new Date(this.getFullYear(), 0, 1).getSeason())
(new Date(beginDate .getFullYear(), 11, 31).getSeason() -
beginDate.getSeason()) 1;
break;
default:
qDiff = (this.getSeason() - new Date( this.getFullYear(), 0, 1).getSeason())
(new Date(beginDate.getFullYear(), 11, 31).getSeason() -
beginDate.getSeason()) 1 (yearDiff - 1) * 4;
break;
}
return qDiff;
case "month":
case "mm":
case "m":
var monthDiff = 0;
switch (yearDiff) {
case 0:
monthDiff = Math.abs(this.getMonth() - beginDate.getMonth());
break;
case 1:
monthDiff = (this.getMonth() - new Date(this.getFullYear(), 0, 1).getMonth())
(new Date(beginDate.getFullYear(), 11, 31).getMonth() -
beginDate.getMonth()) 1;
break;
default:
monthDiff = (this.getMonth() - new Date(this.getFullYear(), 0, 1).getMonth( ))
(new Date(beginDate.getFullYear(), 11, 31).getMonth() -
beginDate.getMonth()) 1 (yearDiff - 1) * 12;
break;
}
return monthDiff;
case "dayofyear":
case "dy":
case "y":
case "day":
case "dd":
case "d":
return Math.abs((this.setHours(0, 0, 0, 0) - beginDate.setHours(0, 0, 0, 0)) / 1000 / 60 / 60 / 24);
case "week":
case "wk":
case "ww":
var weekDiff = 0;
switch (yearDiff) {
case 0:
weekDiff = Math.abs(this.getWeek() - beginDate.getWeek());
break;
case 1:
weekDiff = (this.getWeek() - new Date(this.getFullYear(), 0, 1).getWeek())
(new Date(beginDate.getFullYear(), 11, 31).getWeek() -
beginDate.getWeek()) 1;
break;
default:
weekDiff = (this.getWeek() - new Date(this.getFullYear(), 0, 1).getWeek())
(new Date(beginDate.getFullYear(), 11, 31).getWeek() -
beginDate.getWeek()) 1;
var thisYear = this.getFullYear();
for (var i = 1; i weekDiff = new Date(thisYear - i, 0, 1).getWeeksOfYear();
}
break;
}
return weekDiff;
case "hour":
case "hh":
return Math.abs((this - beginDate) / 1000 / 60 / 60);
case "minute":
case "mi":
case "n":
return Math.abs((this - beginDate) / 1000 / 60);
case "second":
case "ss":
case "s":
return Math.abs( (this - beginDate) / 1000);
case "millisecond":
case "ms":
return Math.abs(this - beginDate);
}
}
Date .prototype.getFirstWeekDays = function() {
///
///Get the number of days in the first week of the year where the current date is located
///
return (7 - new Date(this.getFullYear(), 0, 1).getDay()); //The month in JS also starts from 0, 0 means January, and so on.
}
Date.prototype.getLastWeekDays = function(year) {
///
///Get the number of days in the last week in the year where the current date is located
// /
return (new Date(this.getFullYear(), 11, 31).getDay() 1); //The month in JS also starts from 0, 0 means January, so on analogy.
}
Date.prototype.getWeeksOfYear = function() {
///
///Get the week number of the year in which the current date is located
/// summary>
return (Math.ceil((new Date(this.getFullYear(), 11, 31, 23, 59, 59) -
new Date(this.getFullYear(), 0, 1)) / 1000 / 60 / 60 / 24) -
this.getFirstWeekDays() - this.getLastWeekDays()) / 7 2;
}
Date.prototype.getSeason = function() {
// /
///Get the quarter of the year where the current date is. Returns a quarter integer value.
///
var month = this.getMonth();
switch (month) {
case 0:
case 1:
case 2:
return 1;
case 3:
case 4:
case 5:
return 2;
case 6:
case 7:
case 8:
return 3;
default:
return 4;
}
}
Date.prototype.getWeek = function() {
///
///获取当前日期所在是一年中的第几周。返回一个整数值。
///
var firstDate = new Date(this.getFullYear(), 0, 1);
var firstWeekDays = this.getFirstWeekDays();
var secondWeekFirstDate = firstDate.add("dd", firstWeekDays, true);
var lastDate = new Date(this.getFullYear(), 11, 31);
var lastWeekDays = this.getLastWeekDays();
if (this.dateDiff("day", firstDate) return 1;
}
else if (this.dateDiff("day", lastDate) return this.getWeeksOfYear();
}
else {
return Math.ceil((this - secondWeekFirstDate) / 1000 / 60 / 60 / 24 / 7) 1;
}
}

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development 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.

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment
