Javascript日付オブジェクトDate拡張方法

14790 ワード

今日はネットでjsの操作日付の関連方法を抜粋しました.今は皆さんと分かち合います.
 
  
<br>Date.prototype.Format = function(fmt) <br>{ <br>//author: meizz <br>var o = <br>{ <br>"M+" : this.getMonth() + 1, // <br>"d+" : this.getDate(), // <br>"h+" : this.getHours(), // <br>"m+" : this.getMinutes(), // <br>"s+" : this.getSeconds(), // <br>"q+" : Math.floor((this.getMonth() + 3) / 3), // <br>"S" : this.getMilliseconds() // <br>}; <br>if (/(y+)/.test(fmt)) <br>fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); <br>for (var k in o) <br>if (new RegExp("(" + k + ")").test(fmt)) <br>fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); <br>return fmt; <br>} <br>Date.prototype.addDays = function(d) <br>{ <br>this.setDate(this.getDate() + d); <br>}; <br>Date.prototype.addWeeks = function(w) <br>{ <br>this.addDays(w * 7); <br>}; <br>Date.prototype.addMonths= function(m) <br>{ <br>var d = this.getDate(); <br>this.setMonth(this.getMonth() + m); <br>if (this.getDate() < d) <br>this.setDate(0); <br>}; <br>Date.prototype.addYears = function(y) <br>{ <br>var m = this.getMonth(); <br>this.setFullYear(this.getFullYear() + y); <br>if (m < this.getMonth()) <br>{ <br>this.setDate(0); <br>} <br>}; <br>// var now = new Date(); now.addDays(1);// alert(now.Format("yyyy-MM-dd")); <br>Date.prototype.dateDiff = function(interval,endTime) <br>{ <br>switch (interval) <br>{ <br>case "s": //� <br>return parseInt((endTime-this)/1000); <br>case "n": //� <br>return parseInt((endTime-this)/60000); <br>case "h": //� �r <br>return parseInt((endTime-this)/3600000); <br>case "d": //� <br>return parseInt((endTime-this)/86400000); <br>case "w": //� �L <br>return parseInt((endTime-this)/(86400000*7)); <br>case "m": //� <br>return (endTime.getMonth()+1)+((endTime.getFullYear()-this.getFullYear())*12)-(this.getMonth()+1); <br>case "y": //� <br>return endTime.getFullYear()-this.getFullYear(); <br>default: //� �` <br>return undefined; <br>} <br>} <br>// 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("�r : "+starTime .dateDiff("h",endTime )+"<br>"); document.writeln(" : "+starTime .dateDiff("d",endTime )+"<br>"); document.writeln("�L : "+starTime .dateDiff("w",endTime )+"<br>"); document.writeln(" : "+starTime .dateDiff("m",endTime )+"<br>"); document.writeln(" : "+starTime .dateDiff("y",endTime )+"<br>"); <br>
具体的な拡張方法は以下の通りです.
parseCHS――静的方法.よく使われる中国語の日付を解析し、日付オブジェクトに戻ります.
add--日付の加減操作.[注:この関数はアップロード時にBUGが存在します.ダウンロードしてから、この関数の最初の行の「var regExp=///^\d+/」"""、"、"var regExp=//"([+])?\d+$/""、"に変更してください.そうでなければ、減算はできません."
dateDiffeーー日付が悪いです.開始日と現在の日付の差は、差の絶対値を返します.
get First WeekDaysは、現在の日付がある年の最初の週の日数を取得します.
getLastWeekDays-現在の日付がある年の最後の週の日数を取得します.
get WeeksOfYear-現在の日付のある年の週数を取得します.
get Week-現在の日付を取得するのは一年の中で何週目ですか?整数値を返します.
get Season-現在の日付を取得するのは1年の第数四半期です.四半期の整数値を返します.
詳細な注釈とパラメータはJSファイル内のコメントを参照してください.
 
  
/*
=====================================================================================
Description:Date 。 、 、 、 。
Author:Dezwen.
Date:2009-5-30.
=====================================================================================
*/
Date.parseCHS = function(dateString) {
///
/// 。
///

///
/// 。 :"xxxx(xx)-xx-xx xx:xx:xx","xxxx(xx).xx.xx xx:xx:xx",
///"xxxx(xx) xx xx xx xx xx "
///
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} \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(" ", ":").replace(" ", ":"
).replace(" ", "");
}
else {
throw " Date.parseCHS 。 。";
}
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) {
///
/// 。
/// returnNewObjec true , , ,
/// , .
///

///
/// :
///Year, yy, yyyy--
///quarter, qq, q --
///Month, mm, m --
///dayofyear, dy, y--
///Day, dd, d --
///Week, wk, ww --
///Hour, hh --
///minute, mi, n --
///second, ss, s --
///millisecond, ms --
///
///
///
///
///
/// 。 true , , .
///
///
///
///

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) {
///
/// , 。
///

///
/// :
///Year, yy, yyyy-- ;
///quarter, qq, q --
///Month, mm, m --
///dayofyear, dy, y--
///Day, dd, d --
///Week, wk, ww --
///Hour, hh --
///minute, mi, n --
///second, ss, s --
///millisecond, ms --
///
///
///
///
///
/// 。
///

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() {
///
///
///

return (7 - new Date(this.getFullYear(), 0, 1).getDay()); //JS 0 ,0 1 , 。
}
Date.prototype.getLastWeekDays = function(year) {
///
///
///

return (new Date(this.getFullYear(), 11, 31).getDay() + 1); //JS 0 ,0 1 , 。
}
Date.prototype.getWeeksOfYear = function() {
///
///
///

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() {
///
/// 。 。
///

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;
}
}