w3c對Date 對象的定義:
Date 對象用于處理日期和時間诡渴。
創(chuàng)建Date 對象:
var myDate=new Date();
注釋:Date 對象會自動把當(dāng)前日期和時間保存為其初始值娘摔。
var date = new Date();
console.log(date);
顯示系統(tǒng)默認(rèn)時間
這里要說明的是GMT+0800 (CST)是格林尼治時間。就是我們常常說的0度經(jīng)線的地方平時拴驮,也叫格林平時春瞬。格林平時的英文是Greenwich Mean Time,簡稱GMT套啤,實(shí)際上就是UT(universal time )也叫世界時的宽气。這個里說的+0800,北京時間就是東八區(qū)時區(qū)中線120度經(jīng)線的地方平時潜沦。從0度格林經(jīng)線到120度東八區(qū)時區(qū)經(jīng)線萄涯,經(jīng)差為120度,換算成時間為8小時(120除以15)唆鸡。所以格林平時和北京時間差八小時涝影,而且是北京時間比格林平時快8小時,因此是在格林平時的基礎(chǔ)上加8小時的喇闸。
Date 對象屬性
- constructor 返回對創(chuàng)建此對象的 Date 函數(shù)的引用袄琳。
- prototype 這個是原型讓我們可以向?qū)ο筇砑訉傩院头椒ā?/li>
這里展開分別說一下constructor屬性和prototype
constructor 屬性返回對創(chuàng)建此對象的 Date 函數(shù)的引用。
實(shí)例:
<script type="text/javascript">
var date = new Date();
if (date.constructor==Array) {
document.write("This is an Array");
}
if (date.constructor==Boolean) {
document.write("This is a Boolean");
}
if (date.constructor==Date) {
document.write("This is a Date");
}
if (test.constructor==String){
document.write("This is a String");
}
</script>
輸出:
This is a Date
prototype屬性
prototype 利用prototype原型屬性讓我們有能力向?qū)ο筇砑訉傩院头椒ā?br>
語法如下:
object.prototype.name=value
以下的實(shí)例燃乍,我們將展示如何使用 prototype 屬性來向?qū)ο筇砑訉傩裕?/p>
<script type="text/javascript">
function Person(name,job,born){
this.name=name;
this.job=job;
this.born=born;
}
var person = new Person("zhangsan","人民教師",1985);
Person.prototype.salary=null;
person.salary=20000;
console.log(person.salary);
</script>
輸出:
20000
這里需要注意:對象的屬性可以直接修改對象原型上的屬性
Person.prototype.salary=null;
person.salary=20000;
接下來,讓我們言歸正傳宛琅,來看Date 對象的方法常用的都有哪些刻蟹,下邊的這個表格來自w3c。
方法 | 屬性 |
---|---|
Date() | 返回當(dāng)日的日期和時間嘿辟。 |
getDate() | 從 Date 對象返回一個月中的某一天 (1 ~ 31)舆瘪。 |
getDay() | 從 Date 對象返回一周中的某一天 (0 ~ 6)。 |
getMonth() | 從 Date 對象返回月份 (0 ~ 11)红伦。注意:這里的月份是從索引0開始 |
getFullYear() | 從 Date 對象以四位數(shù)字返回年份英古。 |
getYear() | 請使用 getFullYear() 方法代替。 |
getHours() | 返回 Date 對象的小時 (0 ~ 23)昙读。 |
getMinutes() | 返回 Date 對象的分鐘 (0 ~ 59)召调。 |
getSeconds() | 返回 Date 對象的秒數(shù) (0 ~ 59)。 |
getMilliseconds() | 返回 Date 對象的毫秒(0 ~ 999)。 |
getTime() | 返回 1970 年 1 月 1 日至今的毫秒數(shù)唠叛。 |
getTimezoneOffset() | 返回本地時間與格林威治標(biāo)準(zhǔn)時間 (GMT) 的分鐘差只嚣。 |
setDate() | 設(shè)置 Date 對象中月的某一天 (1 ~ 31)。 |
setMonth() | 設(shè)置 Date 對象中月份 (0 ~ 11)艺沼。 |
setFullYear() | 設(shè)置 Date 對象中的年份(四位數(shù)字)册舞。 |
setYear() | 請使用 setFullYear() 方法代替。 |
setHours() | 設(shè)置 Date 對象中的小時 (0 ~ 23)障般。 |
setMinutes() | 設(shè)置 Date 對象中的分鐘 (0 ~ 59)调鲸。 |
setSeconds() | 設(shè)置 Date 對象中的秒鐘 (0 ~ 59)。 |
setMilliseconds() 方法 | 設(shè)置 Date 對象中的毫秒 (0 ~ 999)挽荡。 |
setTime() | 以毫秒設(shè)置 Date 對象线得。 |
toSource() | 返回該對象的源代碼。 |
toString() | 把 Date 對象轉(zhuǎn)換為字符串徐伐。 |
UTC() | 根據(jù)世界時返回 1970 年 1 月 1 日 到指定日期的毫秒數(shù)贯钩。 |
valueOf() | 返回 Date 對象的原始值。 |
- 下邊是一個獲取年月日星期的例子
<script type="text/javascript">
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var week = date.getDay();
switch(week){
case 0:
str = "星期日";
break;
case 1:
str = "星期一";
break;
case 2:
str = "星期二";
break;
case 3:
str = "星期三";
break;
case 4:
str = "星期四";
break;
case 5:
str = "星期五";
break;
default:
str = "星期六";
}
var current = year + '年' + addZero(month) + '月' + addZero(day) + '日';
console.log("今天是:" + current + ' ' + str);
function addZero(obj){
if(obj<10){
return '0' + obj;
}else{
return obj;
}
}
</script>
輸出:
今天是:2017年06月23日 星期五
- 下邊是一個倒計時的例子
<script type="text/javascript">
function fnTime() {
var time1 = new Date();
var time2 = new Date("2018/05/02 15:00:00");
var countDown = time2 - time1;
var oTime = document.getElementById("time");
var t = countDown / 1000; //將毫秒換算成秒
var day = Math.floor( t / (60*60*24) );
var h = Math.floor( t / (60*60) % 24 );
var m = Math.floor( t / 60 % 60 );
var s = Math.floor( t % 60 );
var str = day + '天' + h + '小時' + m + '分鐘' + s + '秒';
oTime.innerHTML = str;
if(countDown<=0){
clearInterval(timer);
}
}
var timer = setInterval(fnTime,0);
</script>
輸入:
312天23小時1分鐘4秒
結(jié)束
以上是常用的兩個例子办素,另外本文展示了平時在我們工作中用到時的一些手冊或者方法角雷。如有幫助可收藏并查詢,謝謝性穿。