Today I excerpted some related methods of operating dates in js from the Internet, and now I will share them with you.
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
///
///< param name="beginDate" type="DateTime">
///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 < yearDiff; 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) < firstWeekDays) {
return 1;
}
else if (this.dateDiff("day", lastDate) < lastWeekDays) {
return this.getWeeksOfYear();
}
else {
return Math.ceil((this - secondWeekFirstDate) / 1000 / 60 / 60 / 24 / 7) 1;
}
}