ホームページ  >  記事  >  ウェブフロントエンド  >  JSの日付加算・減算関数を整理しました

JSの日付加算・減算関数を整理しました

巴扎黑
巴扎黑オリジナル
2016-12-06 11:07:001473ブラウズ

// 日を追加
function AddDays(date,value)
{
date.setDate(date.getDate()+value);
}
// 月を追加
function AddMonths(date,value)
{
date.setMonth (date.getMonth()+value);
}
// 年を追加します
function Addyears(date,value)
{
date.setFull Year(date.getFull Year()+value)
// 今日ですか
; function IsToday(date)
{
return IsDateEquals(date,new Date());
}
// 現在の月かどうか
function IsThisMonth(date)
{
return IsMonthEquals(date,new Date());
}
// 2 つの日付の年は等しいですか?
function IsMonthEquals(date1,date2)
{
return date1.getMonth()==date2.getMonth()&&date1.getFull Year()==date2.getFull Year( );
}
/ / 日付が等しいかどうかを判断します
function IsDateEquals(date1,date2)
{
return date1.getDate()==date2.getDate()&&IsMonthEquals(date1,date2);
// Return特定の日付に対応する月の日数
function GetMonthDayCount(date)
{
switch(date.getMonth()+1)
{
case 1:case 3:case 5:case 7:case 8:case 10: ケース 12:
リターン 31;
ケース 4 : ケース 6: ケース 9: ケース 11:
リターン 30;
//feb:
var lastd=28; .setDate(29);
while(date.getMonth ()==1)
{
AddDays(date,1);
}
return lastd;
// 2 桁の年を返します。 GetHarf Year(date)
{
var v=date.get Year();
if(v>9)return v.toString();
return "0"+v;
// 月を返します (2 に修正されます)数字)
function GetFullMonth(date)
{
var v=date.getMonth()+1;
if(v>9)return v.toString();
return "0"+v;
// Return日 (2 桁に修正)
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,"d", date.getDate());
return str ;
}
//統一日付形式
function ConvertDate(str)
{
str=(str+"").replace(/^s*/g,"").replace (/s*$/g,"" ); //
var d の前後の空白を削除します
if(/^[0-9]{8}$/.test(str)) // 20040226 - > 2004-02-26
{
d=new Date(new Number(str.substr(0,4)),new Number(str.substr(4,2))-1,new Number(str.substr( 6,2)));
if(d.getTime())return
}
if(d.getTime())return d; str,"-","/"));
if(d.getTime())return d;
js 時差関数