Javascript 日期對(duì)象Date擴(kuò)展方法
更新時(shí)間:2009年05月30日 22:25:03 作者:
Date對(duì)象擴(kuò)展,包括常用中文日期格式解析、加減操作、日期差、周操作和季操作。
今天在網(wǎng)上摘抄了些js中操作日期的相關(guān)方法,現(xiàn)在與大家分享一下。
<script type="text/javascript">
Date.prototype.Format = function(fmt)
{
//author: meizz
var o =
{
"M+" : this.getMonth() + 1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小時(shí)
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth() + 3) / 3), //季度
"S" : this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
Date.prototype.addDays = function(d)
{
this.setDate(this.getDate() + d);
};
Date.prototype.addWeeks = function(w)
{
this.addDays(w * 7);
};
Date.prototype.addMonths= function(m)
{
var d = this.getDate();
this.setMonth(this.getMonth() + m);
if (this.getDate() < d)
this.setDate(0);
};
Date.prototype.addYears = function(y)
{
var m = this.getMonth();
this.setFullYear(this.getFullYear() + y);
if (m < this.getMonth())
{
this.setDate(0);
}
};
//測(cè)試 var now = new Date(); now.addDays(1);//加減日期操作 alert(now.Format("yyyy-MM-dd"));
Date.prototype.dateDiff = function(interval,endTime)
{
switch (interval)
{
case "s": //計(jì)算秒差
return parseInt((endTime-this)/1000);
case "n": //計(jì)算分差
return parseInt((endTime-this)/60000);
case "h": //計(jì)算時(shí)差
return parseInt((endTime-this)/3600000);
case "d": //計(jì)算日差
return parseInt((endTime-this)/86400000);
case "w": //計(jì)算週差
return parseInt((endTime-this)/(86400000*7));
case "m": //計(jì)算月差
return (endTime.getMonth()+1)+((endTime.getFullYear()-this.getFullYear())*12)-(this.getMonth()+1);
case "y": //計(jì)算年差
return endTime.getFullYear()-this.getFullYear();
default: //輸入有誤
return undefined;
}
}
//測(cè)試 var starTime = new Date("2007/05/12 07:30:00"); var endTime = new Date("2008/06/12 08:32:02"); document.writeln("秒差: "+starTime .dateDiff("s",endTime )+"<br>"); document.writeln("分差: "+starTime .dateDiff("n",endTime )+"<br>"); document.writeln("時(shí)差: "+starTime .dateDiff("h",endTime )+"<br>"); document.writeln("日差: "+starTime .dateDiff("d",endTime )+"<br>"); document.writeln("週差: "+starTime .dateDiff("w",endTime )+"<br>"); document.writeln("月差: "+starTime .dateDiff("m",endTime )+"<br>"); document.writeln("年差: "+starTime .dateDiff("y",endTime )+"<br>");
</script>
具體擴(kuò)展的方法如下:
parseCHS--靜態(tài)方法。解析常用的中文日期并返回日期對(duì)象。
add--日期加減操作。[注:此函數(shù)在上傳時(shí)還存在一個(gè)BUG。請(qǐng)下載后把此函數(shù)內(nèi)的第一行"var regExp = /^\d+$/;" 改為 "var regExp = /^([+-])?\d+$/;", 要不然就做不了減法。]
dateDiff--日期差。開始日期與當(dāng)前日期的差,返回差的絕對(duì)值。
getFirstWeekDays--獲取當(dāng)前日期所在年份中第一個(gè)星期的天數(shù)。
getLastWeekDays--獲取當(dāng)前日期所在年份中最后一個(gè)星期的天數(shù)。
getWeeksOfYear--獲取當(dāng)前日期所在年份的周數(shù)。
getWeek--獲取當(dāng)前日期所在是一年中的第幾周。返回一個(gè)整數(shù)值。
getSeason--獲取當(dāng)前日期所在是一年中的第幾季。返回一個(gè)季度整數(shù)值。
詳細(xì)注釋及參數(shù),請(qǐng)參考JS文件內(nèi)的注釋。
/*
=====================================================================================
Description:Date對(duì)象擴(kuò)展。包括常用中文日期格式解析、加減操作、日期差、周操作和季操作。
Author:Dezwen.
Date:2009-5-30.
=====================================================================================
*/
Date.parseCHS = function(dateString) {
///<summary>
///解析常用的中文日期并返回日期對(duì)象。
///</summary>
///<param name="dateString" type="string">
///日期字符串。包含的格式有:"xxxx(xx)-xx-xx xx:xx:xx","xxxx(xx).xx.xx xx:xx:xx",
///"xxxx(xx)年xx月xx日 xx時(shí)xx分xx秒"
///</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}年\d{1,2}月\d{1,2}日( \d{1,2}時(shí)\d{1,2}分\d{1,2}秒)?$/;
if (regExp1.test(dateString)) { }
else if (regExp2.test(dateString)) {
dateString = dateString.replace(/\./g, "-");
}
else if (regExp3.test(dateString)) {
dateString = dateString.replace("年", "-").replace(
"月", "-").replace("日", "").replace("時(shí)", ":").replace("分", ":"
).replace("秒", "");
}
else {
throw "傳給Date.parseCHS的參數(shù)值的格式不正確。請(qǐng)傳遞一個(gè)有效的日期格式字符串作為參數(shù)。";
}
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) {
///<summary>
///日期加減。
///若returnNewObjec參數(shù)為true值,則操作結(jié)果由一個(gè)新的日期對(duì)象返回,原日期對(duì)象不變,
///否則返回的是原日期對(duì)象,此時(shí)原日期對(duì)象的值是操作結(jié)果.
///</summary>
///<param name="datepart" type="string">
///日期加減的部分:
///Year, yy, yyyy--年
///quarter, qq, q --季
///Month, mm, m -- 月
///dayofyear, dy, y-- 日
///Day, dd, d -- 日
///Week, wk, ww -- 周
///Hour, hh -- 小時(shí)
///minute, mi, n -- 分鐘
///second, ss, s -- 秒
///millisecond, ms -- 毫秒
///</param>
///<param name="number" type="int">
///要加減的數(shù)量
///</param>
///<param name="returnNewObjec" type="bool">
///是否返回新的日期對(duì)象。若參數(shù)為true值,則返回一個(gè)新的日期對(duì)象,否則返回的是當(dāng)前日期對(duì)象.
///</param>
///<returns type="Date">
///返回一個(gè)日期對(duì)象
///</returns>
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) {
///<summary>
///開始日期與當(dāng)前日期的差,返回差的絕對(duì)值。
///</summary>
///<param name="datepart" type="string">
///日期加減的部分:
///Year, yy, yyyy--年 ;
///quarter, qq, q --季
///Month, mm, m -- 月
///dayofyear, dy, y-- 日
///Day, dd, d -- 日
///Week, wk, ww -- 周
///Hour, hh -- 小時(shí)
///minute, mi, n -- 分鐘
///second, ss, s -- 秒
///millisecond, ms -- 毫秒
///</param>
///<param name="beginDate" type="DateTime">
///要用于比較我日期
///</param>
///<returns type="int">
///返回日期差的絕對(duì)值。
///</returns>
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() {
///<summary>
///獲取當(dāng)前日期所在年份中第一個(gè)星期的天數(shù)
///</summary>
return (7 - new Date(this.getFullYear(), 0, 1).getDay()); //JS里的月份也是從0開始的,0表示1月,依此類推。
}
Date.prototype.getLastWeekDays = function(year) {
///<summary>
///獲取當(dāng)前日期所在年份中最后一個(gè)星期的天數(shù)
///</summary>
return (new Date(this.getFullYear(), 11, 31).getDay() + 1); //JS里的月份也是從0開始的,0表示1月,依此類推。
}
Date.prototype.getWeeksOfYear = function() {
///<summary>
///獲取當(dāng)前日期所在年份的周數(shù)
///</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() {
///<summary>
///獲取當(dāng)前日期所在是一年中的第幾季。返回一個(gè)季度整數(shù)值。
///</summary>
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() {
///<summary>
///獲取當(dāng)前日期所在是一年中的第幾周。返回一個(gè)整數(shù)值。
///</summary>
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;
}
}
復(fù)制代碼 代碼如下:
<script type="text/javascript">
Date.prototype.Format = function(fmt)
{
//author: meizz
var o =
{
"M+" : this.getMonth() + 1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小時(shí)
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth() + 3) / 3), //季度
"S" : this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
Date.prototype.addDays = function(d)
{
this.setDate(this.getDate() + d);
};
Date.prototype.addWeeks = function(w)
{
this.addDays(w * 7);
};
Date.prototype.addMonths= function(m)
{
var d = this.getDate();
this.setMonth(this.getMonth() + m);
if (this.getDate() < d)
this.setDate(0);
};
Date.prototype.addYears = function(y)
{
var m = this.getMonth();
this.setFullYear(this.getFullYear() + y);
if (m < this.getMonth())
{
this.setDate(0);
}
};
//測(cè)試 var now = new Date(); now.addDays(1);//加減日期操作 alert(now.Format("yyyy-MM-dd"));
Date.prototype.dateDiff = function(interval,endTime)
{
switch (interval)
{
case "s": //計(jì)算秒差
return parseInt((endTime-this)/1000);
case "n": //計(jì)算分差
return parseInt((endTime-this)/60000);
case "h": //計(jì)算時(shí)差
return parseInt((endTime-this)/3600000);
case "d": //計(jì)算日差
return parseInt((endTime-this)/86400000);
case "w": //計(jì)算週差
return parseInt((endTime-this)/(86400000*7));
case "m": //計(jì)算月差
return (endTime.getMonth()+1)+((endTime.getFullYear()-this.getFullYear())*12)-(this.getMonth()+1);
case "y": //計(jì)算年差
return endTime.getFullYear()-this.getFullYear();
default: //輸入有誤
return undefined;
}
}
//測(cè)試 var starTime = new Date("2007/05/12 07:30:00"); var endTime = new Date("2008/06/12 08:32:02"); document.writeln("秒差: "+starTime .dateDiff("s",endTime )+"<br>"); document.writeln("分差: "+starTime .dateDiff("n",endTime )+"<br>"); document.writeln("時(shí)差: "+starTime .dateDiff("h",endTime )+"<br>"); document.writeln("日差: "+starTime .dateDiff("d",endTime )+"<br>"); document.writeln("週差: "+starTime .dateDiff("w",endTime )+"<br>"); document.writeln("月差: "+starTime .dateDiff("m",endTime )+"<br>"); document.writeln("年差: "+starTime .dateDiff("y",endTime )+"<br>");
</script>
具體擴(kuò)展的方法如下:
parseCHS--靜態(tài)方法。解析常用的中文日期并返回日期對(duì)象。
add--日期加減操作。[注:此函數(shù)在上傳時(shí)還存在一個(gè)BUG。請(qǐng)下載后把此函數(shù)內(nèi)的第一行"var regExp = /^\d+$/;" 改為 "var regExp = /^([+-])?\d+$/;", 要不然就做不了減法。]
dateDiff--日期差。開始日期與當(dāng)前日期的差,返回差的絕對(duì)值。
getFirstWeekDays--獲取當(dāng)前日期所在年份中第一個(gè)星期的天數(shù)。
getLastWeekDays--獲取當(dāng)前日期所在年份中最后一個(gè)星期的天數(shù)。
getWeeksOfYear--獲取當(dāng)前日期所在年份的周數(shù)。
getWeek--獲取當(dāng)前日期所在是一年中的第幾周。返回一個(gè)整數(shù)值。
getSeason--獲取當(dāng)前日期所在是一年中的第幾季。返回一個(gè)季度整數(shù)值。
詳細(xì)注釋及參數(shù),請(qǐng)參考JS文件內(nèi)的注釋。
復(fù)制代碼 代碼如下:
/*
=====================================================================================
Description:Date對(duì)象擴(kuò)展。包括常用中文日期格式解析、加減操作、日期差、周操作和季操作。
Author:Dezwen.
Date:2009-5-30.
=====================================================================================
*/
Date.parseCHS = function(dateString) {
///<summary>
///解析常用的中文日期并返回日期對(duì)象。
///</summary>
///<param name="dateString" type="string">
///日期字符串。包含的格式有:"xxxx(xx)-xx-xx xx:xx:xx","xxxx(xx).xx.xx xx:xx:xx",
///"xxxx(xx)年xx月xx日 xx時(shí)xx分xx秒"
///</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}年\d{1,2}月\d{1,2}日( \d{1,2}時(shí)\d{1,2}分\d{1,2}秒)?$/;
if (regExp1.test(dateString)) { }
else if (regExp2.test(dateString)) {
dateString = dateString.replace(/\./g, "-");
}
else if (regExp3.test(dateString)) {
dateString = dateString.replace("年", "-").replace(
"月", "-").replace("日", "").replace("時(shí)", ":").replace("分", ":"
).replace("秒", "");
}
else {
throw "傳給Date.parseCHS的參數(shù)值的格式不正確。請(qǐng)傳遞一個(gè)有效的日期格式字符串作為參數(shù)。";
}
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) {
///<summary>
///日期加減。
///若returnNewObjec參數(shù)為true值,則操作結(jié)果由一個(gè)新的日期對(duì)象返回,原日期對(duì)象不變,
///否則返回的是原日期對(duì)象,此時(shí)原日期對(duì)象的值是操作結(jié)果.
///</summary>
///<param name="datepart" type="string">
///日期加減的部分:
///Year, yy, yyyy--年
///quarter, qq, q --季
///Month, mm, m -- 月
///dayofyear, dy, y-- 日
///Day, dd, d -- 日
///Week, wk, ww -- 周
///Hour, hh -- 小時(shí)
///minute, mi, n -- 分鐘
///second, ss, s -- 秒
///millisecond, ms -- 毫秒
///</param>
///<param name="number" type="int">
///要加減的數(shù)量
///</param>
///<param name="returnNewObjec" type="bool">
///是否返回新的日期對(duì)象。若參數(shù)為true值,則返回一個(gè)新的日期對(duì)象,否則返回的是當(dāng)前日期對(duì)象.
///</param>
///<returns type="Date">
///返回一個(gè)日期對(duì)象
///</returns>
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) {
///<summary>
///開始日期與當(dāng)前日期的差,返回差的絕對(duì)值。
///</summary>
///<param name="datepart" type="string">
///日期加減的部分:
///Year, yy, yyyy--年 ;
///quarter, qq, q --季
///Month, mm, m -- 月
///dayofyear, dy, y-- 日
///Day, dd, d -- 日
///Week, wk, ww -- 周
///Hour, hh -- 小時(shí)
///minute, mi, n -- 分鐘
///second, ss, s -- 秒
///millisecond, ms -- 毫秒
///</param>
///<param name="beginDate" type="DateTime">
///要用于比較我日期
///</param>
///<returns type="int">
///返回日期差的絕對(duì)值。
///</returns>
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() {
///<summary>
///獲取當(dāng)前日期所在年份中第一個(gè)星期的天數(shù)
///</summary>
return (7 - new Date(this.getFullYear(), 0, 1).getDay()); //JS里的月份也是從0開始的,0表示1月,依此類推。
}
Date.prototype.getLastWeekDays = function(year) {
///<summary>
///獲取當(dāng)前日期所在年份中最后一個(gè)星期的天數(shù)
///</summary>
return (new Date(this.getFullYear(), 11, 31).getDay() + 1); //JS里的月份也是從0開始的,0表示1月,依此類推。
}
Date.prototype.getWeeksOfYear = function() {
///<summary>
///獲取當(dāng)前日期所在年份的周數(shù)
///</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() {
///<summary>
///獲取當(dāng)前日期所在是一年中的第幾季。返回一個(gè)季度整數(shù)值。
///</summary>
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() {
///<summary>
///獲取當(dāng)前日期所在是一年中的第幾周。返回一個(gè)整數(shù)值。
///</summary>
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;
}
}
您可能感興趣的文章:
- js內(nèi)置對(duì)象 學(xué)習(xí)筆記
- jsp內(nèi)置對(duì)象及方法詳細(xì)介紹
- javascript內(nèi)置對(duì)象arguments詳解
- JavaScript 內(nèi)置對(duì)象屬性及方法集合
- 淺談JavaScript的內(nèi)置對(duì)象和瀏覽器對(duì)象
- javascript對(duì)象之內(nèi)置對(duì)象Math使用方法
- Javascript日期對(duì)象的dateAdd與dateDiff方法
- JavaScript日期對(duì)象(Date)基本用法示例
- 淺談JavaScript中Date(日期對(duì)象),Math對(duì)象
- JavaScript定時(shí)器詳解及實(shí)例
- javascript中SetInterval與setTimeout的定時(shí)器用法
- JS前端知識(shí)點(diǎn)總結(jié)之內(nèi)置對(duì)象,日期對(duì)象和定時(shí)器相關(guān)操作
相關(guān)文章
JS中處理與當(dāng)前時(shí)間間隔的函數(shù)代碼
因?yàn)楣ぷ餍枰粋€(gè)JS處理時(shí)間的函數(shù),參考網(wǎng)上的一些東東,根據(jù)實(shí)際需要寫了一個(gè),記錄在這里,方便需要的朋友2012-05-05js顯示當(dāng)前系統(tǒng)時(shí)間的代碼
有時(shí)候我們需要用js獲取當(dāng)前系統(tǒng)時(shí)間,那么就可以參考下面的代碼2013-03-03javascript時(shí)區(qū)函數(shù)介紹
在js中的時(shí)區(qū)我們以般講的是關(guān)于格林威治時(shí)間和本地時(shí)間之間的時(shí)差,以分鐘為單位,這和php,asp沒什么區(qū)別下面我們結(jié)合date()函數(shù)來介紹一下js時(shí)區(qū)的相關(guān)問題2012-09-09JS getMonth()日期函數(shù)的值域是0-11
好久沒用JS寫代碼了,結(jié)果在計(jì)算日期時(shí),出現(xiàn)了一個(gè)問題,困擾了我半天。最后終于搞清了問題的根源。2010-02-02兼容IE、firefox以及chrome的js獲取時(shí)間(getFullYear)
今天在寫一個(gè)js獲取年份的時(shí)候發(fā)現(xiàn)在firefox以及chrome瀏覽器下有問題,原來不兼容getYear不過可以通過getFullYear來解決,這里特分享下方便需要的朋友2014-07-07用js實(shí)現(xiàn)頁面顯示當(dāng)前日期和時(shí)間的代碼
js時(shí)間日期顯示效果代碼2008-04-04