Javascript Date常用処理関数
5626 ワード
//
Date.diffDayHourString = function(date1,date2){
var date3;
if(typeof date1 == "object"){
date3=date2.getTime()-date1.getTime(); //
}
else{
date3=date2-date1; //
}
var days=Math.floor(date3/(24*3600*1000));
var leave1=date3%(24*3600*1000); //
var hours=Math.floor(leave1/(3600*1000));
return days+"D "+hours+"H";
}
//
Date.diffHourInt = function(date1,date2){
var date3;
if(typeof date1 == "object"){
date3=date2.getTime()-date1.getTime(); //
}
else{
date3=date2-date1; //
}
var days=Math.floor(date3/(24*3600*1000));
var leave1=date3%(24*3600*1000); //
var hours=Math.floor(leave1/(3600*1000));
return days*24+hours;
}
Date.getTime = function(dateStr/*yyyy-MM-dd HH:mm:ss*/){
var d = new Date(dateStr);
return d.getTime();
}
Date.MONTH_DAYS = [31,0,31,30,31,30,31,31,30,31,30,31];
Date.ENG_MONTH_MAP = {'Jan':1, 'Feb':2, 'Mar':3, 'Apr':4, 'May':5, 'Jun':6, 'Jul':7, 'Aug':8, 'Sep':9, 'Oct':10, 'Nov':11, 'Dec':12};
Date.MONTH_ENG_MAP = ['Jan','Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
//
Date.getMonthDays = function(year,month){
year-=0;month-=0;
if(month==1){
if (0==year%4&&((year%100!=0)||(year%400==0)))return 29
else return 28;
}
else return Date.MONTH_DAYS[month];
}
/**
* ,
*
*
* = +
*
* */
Date.getStartEndDateOfWeek = function(year,numOfWeek,outStr){
year-=0;numOfWeek-=1;
var b = new Date(year,0,1),bDayOfWeek = b.getDay()||7,
startDate = new Date(),endDate = new Date(),
extraDays = (7-bDayOfWeek+1),fullDays;
if(extraDays!=7) {
numOfWeek -= 1;
fullDays = numOfWeek*7+extraDays;
}
else{
fullDays = numOfWeek*7;
}
startDate.setTime(new Date(year,0,1).getTime() + (fullDays <= 0 ? 0 : fullDays*24*60*60*1000));
endDate.setTime(startDate.getTime() + (numOfWeek < 0 && extraDays != 7 ? extraDays-1 : 6 )*24*60*60*1000);
if(endDate.getFullYear() > year){
endDate = new Date(year,11,31);
}
//console.log(startDate.toString(),endDate.toString());
if(outStr){
return (startDate.getMonth()+1)+"."+startDate.getDate()+"~"+(endDate.getMonth()+1)+"."+endDate.getDate();
}
else{
return [startDate,endDate];
}
}
//
Date.prototype.format = function (formatStr) {
var date = this;
/*
: 0
:value- , length-
:
*/
var zeroize = function (value, length) {
if (!length) {
length = 2;
}
value = new String(value);
for (var i = 0, zeros = ''; i < (length - value.length); i++) {
zeros += '0';
}
return zeros + value;
};
return formatStr.replace(/"[^"]*"|'[^']*'|\b(?:d{1,4}|M{1,4}|yy(?:yy)?|([hHmstT])\1?|[lLZ])\b/g, function($0) {
switch ($0) {
case 'd': return date.getDate();
case 'dd': return zeroize(date.getDate());
case 'ddd': return ['Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat'][date.getDay()];
case 'dddd': return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][date.getDay()];
case 'M': return date.getMonth() + 1;
case 'MM': return zeroize(date.getMonth() + 1);
case 'MMM': return ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][date.getMonth()];
case 'MMMM': return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][date.getMonth()];
case 'yy': return new String(date.getFullYear()).substr(2);
case 'yyyy': return date.getFullYear();
case 'h': return date.getHours() % 12 || 12;
case 'hh': return zeroize(date.getHours() % 12 || 12);
case 'H': return date.getHours();
case 'HH': return zeroize(date.getHours());
case 'm': return date.getMinutes();
case 'mm': return zeroize(date.getMinutes());
case 's': return date.getSeconds();
case 'ss': return zeroize(date.getSeconds());
case 'l': return date.getMilliseconds();
case 'll': return zeroize(date.getMilliseconds());
case 'tt': return date.getHours() < 12 ? 'am' : 'pm';
case 'TT': return date.getHours() < 12 ? 'AM' : 'PM';
}
});
}
//
Date.prototype.getMonthWeek = function () {
var date = this, w = date.getDay(), d = date.getDate();
return Math.ceil((d + 6 - w) / 7);
};
//
Date.prototype.getYearWeek = function () {
var date1 = this, date2 = new Date(date1.getFullYear(), 0, 1),
d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000);
return Math.ceil((d + (((date2.getDay()||7) + 1) - 1)) / 7);
};