JavaScript的Date對象的創建和屬性
今天我們來學習一下js中的Date對象。Date 對象主要用於處理日期與時間。
1.Date對象的聲明/創建
// 1.創建日期
var oDate=new Date();
console.log(oDate);
// 2.定義自定義的日期
// new Date(year,month,day,hour,minute,second,millisecond);
var oDate3=new Date(2020,5,27,12,0,0);
console.log(oDate3);
var oDate2=new Date("2020-5-26,12:00:00");
console.log(oDate2);
var oDate4=new Date("2020/5/26,12:00:00");
console.log(oDate4);
var oDate5=new Date(2020);//距離1970年1月1號0點 的毫秒數
console.log(oDate5);
2.Date對象的屬性
// constructor 返回創建此對象的函數引用
console.log(oDate.constructor);
// prototype 向對象添加屬性和方法
// console.log(oDate.getDate());
Date.prototype.func=function(){
console.log(this.getDate())
}
oDate.func();
視頻講解鏈接:
https://www.bilibili.com/video/BV1Tp4y1X7qo/