js中時間日付の処理
2093 ワード
//
function AddDays(date,value)
{
date.setDate(date.getDate()+value);
}
//
function AddMonths(date,value)
{
date.setMonth(date.getMonth()+value);
}
//
function AddYears(date,value)
{
date.setFullYear(date.getFullYear()+value);
}
//
function IsToday(date)
{
return IsDateEquals(date,new Date());
}
//
function IsThisMonth(date)
{
return IsMonthEquals(date,new Date());
}
//
function IsMonthEquals(date1,date2)
{
return date1.getMonth()==date2.getMonth()&&date1.getFullYear()==date2.getFullYear();
}
//
function IsDateEquals(date1,date2)
{
return date1.getDate()==date2.getDate()&&IsMonthEquals(date1,date2);
}
//
function GetMonthDayCount(date)
{
switch(date.getMonth()+1)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
return 31;
case 4:case 6:case 9:case 11:
return 30;
}
//
date=new Date(date);
var lastd=28;
date.setDate(29);
while(date.getMonth()==1)
{
lastd++;
AddDays(date,1);
}
return lastd;
}
//
function GetHarfYear(date)
{
var v=date.getYear();
if(v>9)return v.toString();
return "0"+v;
}
// ( )
function GetFullMonth(date)
{
var v=date.getMonth()+1;
if(v>9)return v.toString();
return "0"+v;
}
// ( )
function GetFullDate(date)
{
var v=date.getDate();
if(v>9)return v.toString();
return "0"+v;
}
//
function Replace(str,from,to)
{
return str.split(from).join(to);
}
//
function FormatDate(date,str)
{
str=Replace(str,"yyyy",date.getFullYear());
str=Replace(str,"MM",GetFullMonth(date));
str=Replace(str,"dd",GetFullDate(date));
str=Replace(str,"yy",GetHarfYear(date));
str=Replace(str,"M",date.getMonth()+1);
str=Replace(str,"d",date.getDate());
return str;
}
参考資料:js日時処理 http://www.studyofnet.com/news/898.html