js獲取當前時間,js時間函式

2021-06-20 19:42:02 字數 3949 閱讀 2473

js獲取當前日期時間及其它操作,js時間函式

var mydate = new date();

mydate.getyear(); //獲取當前年份(2位)

mydate.getfullyear(); //獲取完整的年份(4位,1970-????)

mydate.getmonth(); //獲取當前月份(0-11,0代表1月)

mydate.getdate(); //獲取當前日(1-31)

mydate.getday(); //獲取當前星期x(0-6,0代表星期天)

mydate.gettime(); //獲取當前時間(從1970.1.1開始的毫秒數)

mydate.gethours(); //獲取當前小時數(0-23)

mydate.getminutes(); //獲取當前分鐘數(0-59)

mydate.getseconds(); //獲取當前秒數(0-59)

mydate.getmilliseconds(); //獲取當前毫秒數(0-999)

mydate.tolocaledatestring(); //獲取當前日期

var mytime=mydate.tolocaletimestring(); //獲取當前時間

mydate.tolocalestring( ); //獲取日期與時間

日期時間指令碼庫方法列表

date.prototype.isleapyear 判斷閏年

date.prototype.format 日期格式化

date.prototype.dateadd 日期計算

date.prototype.datediff 比較日期差

date.prototype.tostring 日期轉字串

date.prototype.toarray 日期分割為陣列

date.prototype.datepart 取日期的部分資訊

date.prototype.maxdayofdate 取日期所在月的最大天數

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

stringtodate 字串轉日期型

isvaliddate 驗證日期有效性

checkdatetime 完整日期時間檢查

daysbetween 日期天數差

js**:

//—————————————————

// 判斷閏年

//—————————————————

date.prototype.isleapyear = function()

//—————————————————

// 日期格式化

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

// mm/m 月份

// w/w 星期

// dd/dd/d/d 日期

// hh/hh/h/h 時間

// mm/m 分鐘

// ss/ss/s/s 秒

//—————————————————

date.prototype.format = function(formatstr)

//+—————————————————

//| 求兩個時間的天數差 日期格式為 yyyy-mm-dd

//+—————————————————

function daysbetween(dateone,datetwo)

//+—————————————————

//| 日期計算

//+—————————————————

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

} //+—————————————————

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

//+—————————————————

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

switch (strinterval)

} //+—————————————————

//| 日期輸出字串,過載了系統的tostring方法

//+—————————————————

date.prototype.tostring = function(showweek)

return str;

} //+—————————————————

//| 日期合法性驗證

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

//+—————————————————

function isvaliddate(datestr)

[\-/][\d][\-/][\d]/g,」);

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

}else

return true;

} //+—————————————————

//| 日期時間檢查

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

//+—————————————————

function checkdatetime(str)

)-(\d) (\d):(\d):(\d)$/;

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;

} //+—————————————————

//| 把日期分割成陣列

//+—————————————————

date.prototype.toarray = function()

//+—————————————————

//| 取得日期資料資訊

//| 引數 interval 表示資料型別

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

//+—————————————————

date.prototype.datepart = function(interval)

return partstr;

} //+—————————————————

//| 取得當前日期所在月的最大天數

//+—————————————————

date.prototype.maxdayofdate = function()

//+—————————————————

//| 取得當前日期所在週是一年中的第幾周

//+—————————————————

date.prototype.weeknumofyear = function()

//+—————————————————

//| 字串轉成日期型別

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

//+—————————————————

function stringtodate(datestr)

return mydate;

} 若要顯示:當前日期加時間(如:2009-06-12 12:00)

function curenttime()

js 獲取當前時間

js中獲得當前時間是年份和月份,形如 201208 獲取完整的日期 var date new date var year date.getfullyear var month date.getmonth 1 month month 10 0 month month var mydate year.t...

JS 獲取當前時間

var mydate new date mydate.getyear 獲取當前年份 2位 mydate.getfullyear 獲取完整的年份 4位,1970 mydate.getmonth 獲取當前月份 0 11,0代表1月 mydate.getdate 獲取當前日 1 31 mydate.get...

js獲取當前時間

使用js獲取當前的具體時間和日期 如下 var mydate new date mydate.getyear 獲取當前年份 2位 mydate.getfullyear 獲取完整的年份 4位,1970 mydate.getmonth 獲取當前月份 0 11,0代表1月 mydate.getdate 獲...