JavaScript中的數(shù)據(jù)類型并不多,Date類型就是JavaScript中表示日期的一個數(shù)據(jù)類型。我們可以在日期聲明時格式化。如聲明時獲取日期:

創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計與策劃設(shè)計,崇明網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:崇明等地區(qū)。崇明做網(wǎng)站價格咨詢:18982081108
var today = new Date(); //獲取當(dāng)前時間,并將當(dāng)前時間賦值給today。
var someDay = new Date(1999, 0, 11);//用構(gòu)造函數(shù)初始化Date,時間為1990年1月1日。
//注意表示月份的0~11的數(shù)值,依次表示1~12月。
var anotherDay = new Date(1999, 6, 11, 0, 0, 0);//構(gòu)造函數(shù)的六個參數(shù)依次表示年、月、日、時、分、秒。
若需要修改日期,可以使用的一系列set方法,如setDate()、setMonth()、setFullYear()、setYear()、setHours()、setMinutes()、setSeconds()、setMilliseconds()、setTime()。
與set方法對應(yīng)的,JavaScript也提供了一系列的get方法,這里就不列出,你可以在秒秒學(xué)的日期類型小節(jié)中找到。總之,用Date類型來表示日期是比較合理,也是較為方便的。
1.首先,下載谷歌瀏覽器,
2.下載完成后打開谷歌瀏覽器,然后按【F12】打開谷歌瀏覽器控制臺(在瀏覽器方),然后點擊【Console】切換到調(diào)試窗口
3.然后開始輸入代碼,首先定義一個變量名字為test,代碼如下:
var test =new Date();
這句代碼的意思就是把日期對象賦值給test,這個變量的名字可以任意取,在這里就以test為例了,然后按回車鍵,也就是【Enter】鍵
4.敲回車后出現(xiàn)了一個undefined,這是因為z這只是賦值,沒有任何輸出,所以系統(tǒng)默認打印的信息,接下來,操作test這個變量了,這里面保存的就是當(dāng)前日期,看一下代碼
輸入
test.getFullYear();
然后回車,結(jié)果就是當(dāng)前時間的年份
5.還可以調(diào)用其他函數(shù),來獲取當(dāng)前日期的月份、日、小時、分鐘、秒,代碼如下:
test.getMonth();
test.getDate();
test.getHours();
test.getMinutes();
test.getSeconds();
var data=new Date();
補充:
你到底要干嘛呀 你都不說清楚
如果你想用 javascript顯示日期的話 那就
script language="javascript"
function getMyDate()
{
var now = new Date(); //獲取系統(tǒng)日期
var yy = now.getYear(); //獲取年,
var mm = now.getMonth() + 1; //獲取月
var dd = now.getDate();//獲取該天
document.write(yy+"-"+mm+"-"+dd);
}
/script
html
..
...
..
table
..
..
tdscriptgetMyDate()/script/td
...
...
/table
/html
你這個問題太費勁了,不過終于寫好了
===================================================
script language = "javascript"
/**
* 跟據(jù)年份和月份返回當(dāng)前日期的最大天數(shù)
*/
function getMonthMaxDay(year, month) {
if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
} else if (month != 2) {
return 31;
} else {
if (year % 4 == 0 || (year % 100 == 0 year % 400 == 0)) {
if (month == 2) {
return 29;
}
} else {
if (month == 2) {
return 28;
}
}
}
return 0;
}
function getNextNumDay(nowDate, dayNum){
var intBeginYear = parseInt(nowDate.substring(0, 4),10);
//從"-"后截取月數(shù)
var intBeginMonth = parseInt(nowDate.substring(nowDate.indexOf("-") + 1, nowDate.indexOf("-") + 3),10);
var intBeginDate = parseInt(nowDate.substring(nowDate.lastIndexOf("-") + 1, nowDate.lastIndexOf("-") + 3),10);
var day = new Date(Date.parse(nowDate.replace(/-/g, '/'))); //格式化時間
var week = day.getDay();//獲得今天是周幾
var restDay = parseInt(dayNum/7,10)*2 + dayNum;
var otherDay = dayNum%7;
if(week == 5)//周5加兩天
restDay+=2;
if(week == 6)//周6加一天
restDay+=1;
var nowDay1 = getAfterDay(dayNum,intBeginDate,intBeginMonth,intBeginYear);
var newday = new Date(Date.parse(nowDay1.replace(/-/g, '/'))); //格式化時間
var newWeek = newday.getDay();
if(week == 5)
restDay+=2;
if(week == 6)
restDay+=1;
return getAfterDay(restDay,intBeginDate,intBeginMonth,intBeginYear);
}
function getAfterDay(dayNum,intBeginDate,intBeginMonth,intBeginYear){
date2 = intBeginDate + dayNum;
year2 = intBeginYear;
month2 = intBeginMonth;
maxDate2 = getMonthMaxDay(year2,month2);
if(date2 maxDate2){
date2 = date2 - maxDate2;
month2 += 1;
if(month2 12){
month2 = month2 - 12;
year2 += 1;
}else if(month2 == 12){
month2 = 1;
year2 += 1;
}
}else if(date2 == maxDate2){
date2 = maxDate2;
}
if(date2 getMonthMaxDay(year2,month2)){
getAfterDay(date2,1,month2,year2)//如果減去日期后還是大于下月的天數(shù)則遞歸調(diào)用
}
if(parseInt(date2,10) 10){
date2 = '0' + parseInt(date2,10);
}
if(parseInt(month2,10) 10){
month2 = '0' + parseInt(month2,10);
}
var resultDate2 = year2+"-"+month2+"-"+date2;
return resultDate2;
}
alert(getNextNumDay('2011-01-22',9));
/script
本文標(biāo)題:日期javascript,日期計算器
URL鏈接:http://chinadenli.net/article27/dsidijj.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、Google、網(wǎng)站設(shè)計、網(wǎng)站維護、做網(wǎng)站、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)