SoFunction
Updated on 2025-04-07

Javascript date object Date extension method


/*
=====================================================================================
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.
=====================================================================================
*/
= function(dateString) {
///<summary>
///Analyze commonly used Chinese dates and return the date object.
///</summary>
///<param name="dateString" type="string">
///Date string. The formats contained are: "xxxx(xx)-xx-xx xx:xx:xx","xxxx(xx). xx:xx:xx",
///"xxx (xx) year xx month xx day xx hour xx minutes xx seconds"
///</param>
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}time\d{1,2}minutes\d{1,2}seconds)?$/;
if ((dateString)) { }
else if ((dateString)) {
dateString = (/\./g, "-");
}
else if ((dateString)) {
dateString = ("year", "-").replace(
"Moon", "-").replace("Day", "").replace("Time", ":").replace("Finite", ":"
).replace("second", "");
}
else {
throw "The parameter value passed to is incorrect in format. Please pass a valid date format string as parameter.";
}
var date_time = (" ");
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]);
}
}
= function(datepart, number, returnNewObjec) {
///<summary>
/// Add or subtract dates.
///If the returnNewObject parameter is true, the operation result will be returned by a new date object, and the original date object will not change.
/// Otherwise, the returned original date object, and the value of the original date object is the result of the operation.
///</summary>
///<param name="datepart" type="string">
///The part of date addition and subtraction:
///Year, yy, yyyy--Year
///quarter, qq, q --Queen
///Month, mm, m -- month
///dayofyear, dy, y--
///Day, dd, d -- Day
///Week, wk, ww -- Week
///Hour, hh -- Hours
///minute, mi, n -- minutes
///second, s, s -- seconds
///millisecond, ms -- millisecond
///</param>
///<param name="number" type="int">
///The number of to be added or subtracted
///</param>
///<param name="returnNewObjec" type="bool">
///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.
///</param>
///<returns type="Date">
///Return a date object
///</returns>
var regExp = /^\d+$/;
if ((number)) {
number = parseInt(number);
}
else { number = 0; }
datepart = ();
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":
(() + number);
break;
case "quarter":
case "qq":
case "q":
(() + (number * 3));
break;
case "month":
case "mm":
case "m":
(() + number);
break;
case "dayofyear":
case "dy":
case "y":
case "day":
case "dd":
case "d":
(() + number);
break;
case "week":
case "wk":
case "ww":
(() + (number * 7));
break;
case "hour":
case "hh":
(() + number);
break
case "minute":
case "mi":
case "n":
(() + number);
break
case "second":
case "ss":
case "s":
(() + number);
break;
case "millisecond":
case "ms":
(() + number);
break;
}
return tDate;
}
= function(datepart, beginDate) {
///<summary>
///The difference between the start date and the current date returns the absolute value of the difference.
///</summary>
///<param name="datepart" type="string">
///The part of date addition and subtraction:
///Year, yy, yyy--Year;
///quarter, qq, q --Queen
///Month, mm, m -- month
///dayofyear, dy, y--
///Day, dd, d -- Day
///Week, wk, ww -- Week
///Hour, hh -- Hours
///minute, mi, n -- minutes
///second, s, s -- seconds
///millisecond, ms -- millisecond
///</param>
///<param name="beginDate" type="DateTime">
///To be used to compare my date
///</param>
///<returns type="int">
///Return the absolute value of the date difference.
///</returns>
datepart = ();
var yearDiff = (() - ());
switch (datepart) {
case "year":
case "yy":
case "yyyy":
return yearDiff;
case "quarter":
case "qq":
case "q":
var qDiff = 0;
switch (yearDiff) {
case 0:
qDiff = (() - ());
break;
case 1:
qDiff = (() - new Date((), 0, 1).getSeason()) +
(new Date((), 11, 31).getSeason() -
()) + 1;
break;
default:
qDiff = (() - new Date((), 0, 1).getSeason()) +
(new Date((), 11, 31).getSeason() -
()) + 1 + (yearDiff - 1) * 4;
break;
}
return qDiff;
case "month":
case "mm":
case "m":
var monthDiff = 0;
switch (yearDiff) {
case 0:
monthDiff = (() - ());
break;
case 1:
monthDiff = (() - new Date((), 0, 1).getMonth()) +
(new Date((), 11, 31).getMonth() -
()) + 1;
break;
default:
monthDiff = (() - new Date((), 0, 1).getMonth()) +
(new Date((), 11, 31).getMonth() -
()) + 1 + (yearDiff - 1) * 12;
break;
}
return monthDiff;
case "dayofyear":
case "dy":
case "y":
case "day":
case "dd":
case "d":
return (((0, 0, 0, 0) - (0, 0, 0, 0)) / 1000 / 60 / 60 / 24);
case "week":
case "wk":
case "ww":
var weekDiff = 0;
switch (yearDiff) {
case 0:
weekDiff = (() - ());
break;
case 1:
weekDiff = (() - new Date((), 0, 1).getWeek()) +
(new Date((), 11, 31).getWeek() -
()) + 1;
break;
default:

weekDiff = (() - new Date((), 0, 1).getWeek()) +
(new Date((), 11, 31).getWeek() -
()) + 1;
var thisYear = ();
for (var i = 1; i < yearDiff; i++) {
weekDiff += new Date(thisYear - i, 0, 1).getWeeksOfYear();
}
break;
}
return weekDiff;
case "hour":
case "hh":
return ((this - beginDate) / 1000 / 60 / 60);
case "minute":
case "mi":
case "n":
return ((this - beginDate) / 1000 / 60);
case "second":
case "ss":
case "s":
return ((this - beginDate) / 1000);
case "millisecond":
case "ms":
return (this - beginDate);
}
}
= function() {
///<summary>
/// Get the number of days in the first week of the year the current date is located
///</summary>
return (7 - new Date((), 0, 1).getDay()); //The month in JS also starts from 0, 0 means January, and so on.
}
= function(year) {
///<summary>
/// Get the number of days of the last week in the year the current date is located
///</summary>
return (new Date((), 11, 31).getDay() + 1); //The month in JS also starts from 0, 0 means January, and so on.
}
= function() {
///<summary>
///Get the number of weeks of the year the current date is located
///</summary>
return (((new Date((), 11, 31, 23, 59, 59) -
new Date((), 0, 1)) / 1000 / 60 / 60 / 24) -
() - ()) / 7 + 2;
}
= function() {
///<summary>
///Get what season of the year the current date is. Returns a quarterly integer value.
///</summary>
var month = ();
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;
}
}
= function() {
///<summary>
///Get what week of the year the current date is. Returns an integer value.
///</summary>
var firstDate = new Date((), 0, 1);
var firstWeekDays = ();
var secondWeekFirstDate = ("dd", firstWeekDays, true);
var lastDate = new Date((), 11, 31);
var lastWeekDays = ();
if (("day", firstDate) < firstWeekDays) {
return 1;
}
else if (("day", lastDate) < lastWeekDays) {
return ();
}
else {
return ((this - secondWeekFirstDate) / 1000 / 60 / 60 / 24 / 7) + 1;
}
}