js獲取當(dāng)前日期時(shí)間及其它操作匯總_javascript技巧
來(lái)源:懂視網(wǎng)
責(zé)編:小采
時(shí)間:2020-11-27 21:37:14
js獲取當(dāng)前日期時(shí)間及其它操作匯總_javascript技巧
js獲取當(dāng)前日期時(shí)間及其它操作匯總_javascript技巧: js獲取當(dāng)前日期時(shí)間及其它操作匯總 var myDate = new Date(); myDate.getYear(); //獲取當(dāng)前年份(2位) myDate.getFullYear(); //獲取完整的年份(4位,1970-????) myDate.getMonth(); //獲取當(dāng)前月份
導(dǎo)讀js獲取當(dāng)前日期時(shí)間及其它操作匯總_javascript技巧: js獲取當(dāng)前日期時(shí)間及其它操作匯總 var myDate = new Date(); myDate.getYear(); //獲取當(dāng)前年份(2位) myDate.getFullYear(); //獲取完整的年份(4位,1970-????) myDate.getMonth(); //獲取當(dāng)前月份
js獲取當(dāng)前日期時(shí)間及其它操作匯總
日期時(shí)間腳本庫(kù)方法列表
js代碼:
//---------------------------------------------------
輸出字符串,重載了系統(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;
//如果格式滿(mǎn)足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ō)明格式滿(mǎn)足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ù)類(lèi)型
//| 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 = Datue(''+month+'-'+day+'-'+year+'') \n');
document.write('result = DatePart('ww', myDate) \n');
document.write(' \n');
return result;
}
//+---------------------------------------------------
//| 字符串轉(zhuǎn)成日期類(lèi)型
//| 格式 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;
}
若要顯示:當(dāng)前日期加時(shí)間(如:2009-06-12 12:00)
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
js獲取當(dāng)前日期時(shí)間及其它操作匯總_javascript技巧
js獲取當(dāng)前日期時(shí)間及其它操作匯總_javascript技巧: js獲取當(dāng)前日期時(shí)間及其它操作匯總 var myDate = new Date(); myDate.getYear(); //獲取當(dāng)前年份(2位) myDate.getFullYear(); //獲取完整的年份(4位,1970-????) myDate.getMonth(); //獲取當(dāng)前月份