javascript日期時(shí)間函數(shù)
來(lái)源:易賢網(wǎng) 閱讀:1095 次 日期:2016-06-27 10:17:41
溫馨提示:易賢網(wǎng)小編為您整理了“javascript日期時(shí)間函數(shù)”,方便廣大網(wǎng)友查閱!

javascript日期時(shí)間函數(shù)(全)

日期時(shí)間腳本庫(kù)方法列表

date.prototype.isleapyear 判斷閏年

date.prototype.format 日期格式化

date.prototype.dateadd 日期計(jì)算

date.prototype.datediff 比較日期差

date.prototype.tostring 日期轉(zhuǎn)字符串

date.prototype.toarray 日期分割為數(shù)組

date.prototype.datepart 取日期的部分信息

date.prototype.maxdayofdate 取日期所在月的最大天數(shù)

date.prototype.weeknumofyear 判斷日期所在年的第幾周

stringtodate 字符串轉(zhuǎn)日期型

isvaliddate 驗(yàn)證日期有效性

checkdatetime 完整日期時(shí)間檢查

daysbetween 日期天數(shù)差

js 代碼

//---------------------------------------------------

// 判斷閏年

//---------------------------------------------------

date.prototype.isleapyear = function()

{

return (0==this.getyear()%4&&((this.getyear()%100!=0)||(this.getyear()%400==0)));

}

//---------------------------------------------------

// 日期格式化

// 格式 yyyy/yyyy/yy/yy 表示年份

// mm/m 月份

// w/w 星期

// dd/dd/d/d 日期

// hh/hh/h/h 時(shí)間

// mm/m 分鐘

// ss/ss/s/s 秒

//---------------------------------------------------

date.prototype.format = function(formatstr)

{

var str = formatstr;

var week = ['日','一','二','三','四','五','六'];

str=str.replace(/yyyy|yyyy/,this.getfullyear());

str=str.replace(/yy|yy/,(this.getyear() % 100)>9?(this.getyear() % 100).tostring():'0' + (this.getyear() % 100));

str=str.replace(/mm/,this.getmonth()>9?this.getmonth().tostring():'0' + this.getmonth());

str=str.replace(/m/g,this.getmonth());

str=str.replace(/w|w/g,week[this.getday()]);

str=str.replace(/dd|dd/,this.getdate()>9?this.getdate().tostring():'0' + this.getdate());

str=str.replace(/d|d/g,this.getdate());

str=str.replace(/hh|hh/,this.gethours()>9?this.gethours().tostring():'0' + this.gethours());

str=str.replace(/h|h/g,this.gethours());

str=str.replace(/mm/,this.getminutes()>9?this.getminutes().tostring():'0' + this.getminutes());

str=str.replace(/m/g,this.getminutes());

str=str.replace(/ss|ss/,this.getseconds()>9?this.getseconds().tostring():'0' + this.getseconds());

str=str.replace(/s|s/g,this.getseconds());

return str;

}

//+---------------------------------------------------

//| 求兩個(gè)時(shí)間的天數(shù)差 日期格式為 yyyy-mm-dd

//+---------------------------------------------------

function daysbetween(dateone,datetwo)

{

var onemonth = dateone.substring(5,dateone.lastindexof ('-'));

var oneday = dateone.substring(dateone.length,dateone.lastindexof ('-')+1);

var oneyear = dateone.substring(0,dateone.indexof ('-'));

var twomonth = datetwo.substring(5,datetwo.lastindexof ('-'));

var twoday = datetwo.substring(datetwo.length,datetwo.lastindexof ('-')+1);

var twoyear = datetwo.substring(0,datetwo.indexof ('-'));

var cha=((date.parse(onemonth+'/'+oneday+'/'+oneyear)- date.parse(twomonth+'/'+twoday+'/'+twoyear))/86400000);

return math.abs(cha);

}

//+---------------------------------------------------

//| 日期計(jì)算

//+---------------------------------------------------

date.prototype.dateadd = function(strinterval, number) {

var dttmp = this;

switch (strinterval) {

case 's' :return new date(date.parse(dttmp) + (1000 * number));

case 'n' :return new date(date.parse(dttmp) + (60000 * number));

case 'h' :return new date(date.parse(dttmp) + (3600000 * number));

case 'd' :return new date(date.parse(dttmp) + (86400000 * number));

case 'w' :return new date(date.parse(dttmp) + ((86400000 * 7) * number));

case 'q' :return new date(dttmp.getfullyear(), (dttmp.getmonth()) + number*3, dttmp.getdate(), dttmp.gethours(), dttmp.getminutes(), dttmp.getseconds());

case 'm' :return new date(dttmp.getfullyear(), (dttmp.getmonth()) + number, dttmp.getdate(), dttmp.gethours(), dttmp.getminutes(), dttmp.getseconds());

case 'y' :return new date((dttmp.getfullyear() + number), dttmp.getmonth(), dttmp.getdate(), dttmp.gethours(), dttmp.getminutes(), dttmp.getseconds());

}

}

//+---------------------------------------------------

//| 比較日期差 dtend 格式為日期型或者 有效日期格式字符串

//+---------------------------------------------------

date.prototype.datediff = function(strinterval, dtend) {

var dtstart = this;

if (typeof dtend == 'string' )//如果是字符串轉(zhuǎn)換為日期型

{

dtend = stringtodate(dtend);

}

switch (strinterval) {

case 's' :return parseint((dtend - dtstart) / 1000);

case 'n' :return parseint((dtend - dtstart) / 60000);

case 'h' :return parseint((dtend - dtstart) / 3600000);

case 'd' :return parseint((dtend - dtstart) / 86400000);

case 'w' :return parseint((dtend - dtstart) / (86400000 * 7));

case 'm' :return (dtend.getmonth()+1)+((dtend.getfullyear()-dtstart.getfullyear())*12) - (dtstart.getmonth()+1);

case 'y' :return dtend.getfullyear() - dtstart.getfullyear();

}

}

//+---------------------------------------------------

//| 日期輸出字符串,重載了系統(tǒng)的tostring方法

//+---------------------------------------------------

date.prototype.tostring = function(showweek)

{

var mydate= this;

var str = mydate.tolocaledatestring();

if (showweek)

{

var week = ['日','一','二','三','四','五','六'];

str += ' 星期' + week[mydate.getday()];

}

return str;

}

//+---------------------------------------------------

//| 日期合法性驗(yàn)證

//| 格式為:yyyy-mm-dd或yyyy/mm/dd

//+---------------------------------------------------

function isvaliddate(datestr)

{

var sdate=datestr.replace(/(^\s+|\s+$)/g,''); //去兩邊空格;

if(sdate=='') return true;

//如果格式滿足yyyy-(/)mm-(/)dd或yyyy-(/)m-(/)dd或yyyy-(/)m-(/)d或yyyy-(/)mm-(/)d就替換為''

//數(shù)據(jù)庫(kù)中,合法日期可以是:yyyy-mm/dd(2003-3/21),數(shù)據(jù)庫(kù)會(huì)自動(dòng)轉(zhuǎn)換為yyyy-mm-dd格式

var s = sdate.replace(/[\d]{ 4,4 }[\-/]{ 1 }[\d]{ 1,2 }[\-/]{ 1 }[\d]{ 1,2 }/g,'');

if (s=='') //說(shuō)明格式滿足yyyy-mm-dd或yyyy-m-dd或yyyy-m-d或yyyy-mm-d

{

var t=new date(sdate.replace(/\-/g,'/'));

var ar = sdate.split(/[-/:]/);

if(ar[0] != t.getyear() || ar[1] != t.getmonth()+1 || ar[2] != t.getdate())

{

//alert('錯(cuò)誤的日期格式!格式為:yyyy-mm-dd或yyyy/mm/dd。注意閏年。');

return false;

}

}

else

{

//alert('錯(cuò)誤的日期格式!格式為:yyyy-mm-dd或yyyy/mm/dd。注意閏年。');

return false;

}

return true;

}

//+---------------------------------------------------

//| 日期時(shí)間檢查

//| 格式為:yyyy-mm-dd hh:mm:ss

//+---------------------------------------------------

function checkdatetime(str)

{

var reg = /^(\d+)-(\d{ 1,2 })-(\d{ 1,2 }) (\d{ 1,2 }):(\d{ 1,2 }):(\d{ 1,2 })$/;

var r = str.match(reg);

if(r==null)return false;

r[2]=r[2]-1;

var d= new date(r[1],r[2],r[3],r[4],r[5],r[6]);

if(d.getfullyear()!=r[1])return false;

if(d.getmonth()!=r[2])return false;

if(d.getdate()!=r[3])return false;

if(d.gethours()!=r[4])return false;

if(d.getminutes()!=r[5])return false;

if(d.getseconds()!=r[6])return false;

return true;

}

//+---------------------------------------------------

//| 把日期分割成數(shù)組

//+---------------------------------------------------

date.prototype.toarray = function()

{

var mydate = this;

var myarray = array();

myarray[0] = mydate.getfullyear();

myarray[1] = mydate.getmonth();

myarray[2] = mydate.getdate();

myarray[3] = mydate.gethours();

myarray[4] = mydate.getminutes();

myarray[5] = mydate.getseconds();

return myarray;

}

//+---------------------------------------------------

//| 取得日期數(shù)據(jù)信息

//| 參數(shù) interval 表示數(shù)據(jù)類型

//| y 年 m月 d日 w星期 ww周 h時(shí) n分 s秒

//+---------------------------------------------------

date.prototype.datepart = function(interval)

{

var mydate = this;

var partstr='';

var week = ['日','一','二','三','四','五','六'];

switch (interval)

{

case 'y' :partstr = mydate.getfullyear();break;

case 'm' :partstr = mydate.getmonth()+1;break;

case 'd' :partstr = mydate.getdate();break;

case 'w' :partstr = week[mydate.getday()];break;

case 'ww' :partstr = mydate.weeknumofyear();break;

case 'h' :partstr = mydate.gethours();break;

case 'n' :partstr = mydate.getminutes();break;

case 's' :partstr = mydate.getseconds();break;

}

return partstr;

}

//+---------------------------------------------------

//| 取得當(dāng)前日期所在月的最大天數(shù)

//+---------------------------------------------------

date.prototype.maxdayofdate = function()

{

var mydate = this;

var ary = mydate.toarray();

var date1 = (new date(ary[0],ary[1]+1,1));

var date2 = date1.dateadd(1,'m',1);

var result = datediff(date1.format('yyyy-mm-dd'),date2.format('yyyy-mm-dd'));

return result;

}

//+---------------------------------------------------

//| 取得當(dāng)前日期所在周是一年中的第幾周

//+---------------------------------------------------

date.prototype.weeknumofyear = function()

{

var mydate = this;

var ary = mydate.toarray();

var year = ary[0];

var month = ary[1]+1;

var day = ary[2];

document.write('< script language=vbscript\> \n');

document.write('mydate = datevalue(''+month+'-'+day+'-'+year+'') \n');

document.write('result = datepart('ww', mydate) \n');

document.write(' \n');

return result;

}

//+---------------------------------------------------

//| 字符串轉(zhuǎn)成日期類型

//| 格式 mm/dd/yyyy mm-dd-yyyy yyyy/mm/dd yyyy-mm-dd

//+---------------------------------------------------

function stringtodate(datestr)

{

var converted = date.parse(datestr);

var mydate = new date(converted);

if (isnan(mydate))

{

//var delimcahar = datestr.indexof('/')!=-1?'/':'-';

var arys= datestr.split('-');

mydate = new date(arys[0],--arys[1],arys[2]);

}

return mydate;

}

更多信息請(qǐng)查看網(wǎng)頁(yè)制作
易賢網(wǎng)手機(jī)網(wǎng)站地址:javascript日期時(shí)間函數(shù)
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
相關(guān)閱讀網(wǎng)頁(yè)制作

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)