首頁 > web前端 > js教程 > 主體

JS日期相加減函數整理

巴扎黑
發布: 2016-11-25 11:07:50
原創
1125 人瀏覽過

// 增加天 
function AddDays(date,value) 

date.setDate(date.getDate()+value); 

//增加月(date.getMonth()+value); 

// 增加年 
function AddYears(date,value) 

date.setFullYear(date.getullYear()+value); function IsToday(date) 

return IsDateEquals(date,new Date()); 

// 是否為當月 
function IsThisMonth(date) 
]
// 兩個日期的年是否相等 
function IsMonthEquals(date1,date2) 

return date1.getMonth()==date2.getMonth()&&date1.getFullYear()==date2.getFullYear()&&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 3:case 5:case 7:case 8:case 10:case 3:case 5:case 7:case 8:case 10:case 1212 月:case 6:case 9:case 11: 
return 30; 

//feb: 
date=new Date(date); 
var lastd=28; 
()==1) 

lastd++; 
AddDays(date,1); 

return lastd; 

// 返回兩位數的年份問
function Get getYear(); 
if(v>9)return v.toString(); 
return "0"+v; 

// 返回月份(修正為兩位數) 
function GetFullMonth(date) 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; 

// 替換字串 
ftion 
, from,to) 

return str.split(from).join(to); 

// 格式化日期的表示 
function FormatDate(date,str) 

function FormatDate(date,str) 

function FormatDate"=Replace( ,date.getFullYear()); 
str=Replace(str,"MM",GetFullMonth(date)); 
str=Replace(str,"dd",GetFullDate(date)); 
=Replace(str," yy",GetHarfYear(date)); 
str=Replace(str,"M",date.getMonth()+1); 
str=Replace(str,"d",date.getDate()); 
return strstr ; 

// 統一日期格式 
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 Dated=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 d; 

d=new Date(str); 
if(d.getTime())return d; 
d=new Date(Replace(str,"-","/")); 
if (d.getTime())return d; 
return null; 
}  
js時間差函數 

相關標籤:
js
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!