日期物件
學習**:
查文件math物件
屬性、方法名
功能math.pi
圓周率math.floor()
向下取整
math.ceil()
向上取整
math.round()
四捨五入版 就近取整 注意 -3.5 結果是 -3
math.abs()
絕對值math.max()/math.min()
求最大和最小值
math.random()
獲取範圍在[0,1)內的隨機值
注釋
math.max()
案例
var mymath =
}return max;},
min:
function()
}return min;}}
console.
log(mymath.pi)
; console.
log(mymath.
max(1,
5,9)
);console.
log(mymath.
min(1,
5,9)
);
math.abs()math.
abs(
'-1');
// 1
math.
abs(-2
);// 2
math.
abs(
null);
// 0
math.
abs(
"string");
// nan
math.
abs();
// nan
math.
abs(
'1')
;// 1 隱式轉換,會把字串型-1轉換為數字型
math.
abs(
'abc');
// nan
math.floor()math.
floor
(1.1);
// 1
math.
floor
(1.9);
//1
math.round()
math.
round
(0.5);
// 1
math.
round(-
0.5)
;// 0
math.
round
(3.5);
// 4
math.
round(-
3.5)
;// -3
math.random()
案例
//獲取指定範圍內的隨機整數
function
getrandom
(min, max)
// 想要得到兩個數之間的隨機整數,並且包含這2個整數
function
getrandom
(min, max)
console.
log(
getrandom(1
,10))
;// 隨機點名
var arr =
['張三'
,'張三丰'
,'張三瘋子'
,'李四'
,'李思思'
,'pink老師'];
console.
log(arr[
getrandom(0
, arr.length -1)
]); console.
log(arr[
parseint
(math.
random()
* arr.length)])
;
日期物件
使用date例項化日期物件
var 物件名=
newdate()
;
注釋如果建立例項時並未傳入引數,則得到的日期物件是當前時間對應的日期物件
var date =
newdate()
; console.
log(date)
;// 當前時間對應的日期
示例
var date =
newdate
('2020-6-6 6:6:6');
console.
log(date)
;var date1 =
newdate
(2020,6
,6,6
,6,6
);console.
log(date1)
;// 比實際日期要大些
使用date例項的方法和屬性
示例
// 獲取當前日期的月份
var date =
newdate
('2020-6-6 6:6:6');
console.
log(date.
getmonth()
+1);
// 6 因為getmonth()返回的月份小1月,所以記得+1
// 獲取當前日期的秒鐘
var date =
newdate
('2020-6-6 6:6:6');
console.
log(date.
getseconds()
);// 6
案例
// 封裝乙個函式返回當前年月日 時分秒,格式:2023年66月66日 66:66:66
function
gettime()
console.
log(
gettime()
);
案例
// 1、
var now =
newdate()
;// 例項化date物件
// 用於獲取物件的原始值
console.
log(date.
valueof()
);// 就是我們現在時間距離1970.1.1 總的毫秒數
console.
log(date.
gettime()
);// 2、簡單寫法
var now =
+new
date()
;// +new date() 返回的就是總的毫秒數
console.
log(now)
;// 3、html5中提供的方法,有相容性問題
console.
log(date.
now())
;
function
countdown
(time)
else
} console.
log(
countdown
('2020-6-6 6:6:6'))
;var date =
newdate()
; console.
log(
'當前的時間是:'
+ date)
;
js基礎 內建物件(Math)
math 物件不是建構函式,直接使用即可。ps math.round 四捨五入 5特殊,無論正數還是負數,都是往大數取 1 math.pi console.log math.pi 2 math.max console.log math.max 3,1,8 8 console.log math.max...
JS內建物件
1.什麼是物件 js中的所以事物都是物件 字串 數值 陣列 函式。每個物件都帶屬性和方法 js中允許自定義物件 2.自定義物件 1 定義並建立物件例項 2 使用函式來建立物件,然後建立新的物件例項。兩種自定義物件事例如下 3.string 物件 string物件用於處理已有的字串 字串可以單引號或雙...
js 內建物件
陣列長度從0開始算起 如何建立陣列 建立陣列的基本方法有兩種 1.使用array建構函式 語法 new array 小括號 說明 與想知道要儲存的專案數量 2.向array建構函式中傳遞陣列應包含的項 2.使用陣列字面量表示法 有一對包含陣列項的方括號 表示,多個陣列以逗號隔開 concat 語法 ...