web.jpeg
1. Date()日期函數(shù)
必須通過
new
關鍵字創(chuàng)建實例對象,沒有字面量的形式創(chuàng)建一個 當前的日期對象
//必須通過 new 創(chuàng)建 沒有 字面量的形式
var date1 = new Date();
console.log(date1);
- 創(chuàng)建一個指定日期的對象
//創(chuàng)建一個指定時間的 對象
var d2 = new Date("09/06/2019 10:14:30");
console.log(d2);
- 一些常用的Date()類的方法
d2.getDate();
d2.getDay () 獲取星期 0-6
d2.getMonth () 獲取月 0-11
d2.getFullYear () 獲取完整年份(瀏覽器都支持)
d2.getHours () 獲取小時 0-23
d2.getMinutes () 獲取分鐘 0-59
d2.getSeconds () 獲取秒 0-59
d2.getMilliseconds () 獲取當前的毫秒
d2.getTime () 返回累計毫秒數(shù)(從1970/1/1午夜)
- 格式化當前時間
//格式化時間
function getDateFormat (date) {
//獲取年
var year = date.getFullYear();
//獲取月
var month = date.getMonth() + 1;
//獲取日
var day = date.getDate();
//獲取小時
var hours = date.getHours();
//獲取分鐘
var minute = date.getMinutes();
//獲取秒
var second = date.getSeconds();
//
month = (month < 10 ? "0" + month : month);
day = (day < 10 ? "0" + day : day);
hours = (hours < 10 ? "0" + hours : hours);
minute = (minute <10 ? "0" + minute : minute);
second = (second < 10 ? "0" + second : second);
return year + "年" + month + "月" + day+ "日" + " " + hours + ":" + minute +":" + second;
}
Math函數(shù)
//數(shù)學函數(shù)
//1.取整函數(shù) 去大的 向上取整
var num = Math.ceil(1.02);
console.log(num); 結果:2
//2.向下取整
var num1 = Math.floor(1.5);
console.log(num1); 結果是:1
//3.四舍五入
var num2 = Math.round(1.6);
console.log(num2); 結果是:2
想了解更多可以看Math函數(shù)文檔。
- 仿Math函數(shù)求Max最大值
function MyMath() {
this.getMax = function () {
var max = arguments[0];
for (var i = 0; i < arguments.length; i ++){
if (max<arguments[i]){
max = arguments[i];
}
}
return max;
};
}
var mt = new MyMath();
var result = mt.getMax(10,20,33,45,66);
console.log(result);
- 隨機16進制的顏色
function getColor () {
var str = "#";
//定義數(shù)組
var array = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
for (var i = 0; i < 6; i++) {
//0-15個數(shù)隨機 包括15在內
var num =parseInt(Math.random() *16);
//根據(jù)下標 獲取對象的 array的數(shù)據(jù)
str += array[num];
}
return str;
}
console.log(getColor());
取整函數(shù)
1.向上取整 函數(shù)
//1.取整函數(shù) 去大的 向上取整
var num = Math.ceil(1.02);
console.log(num);
// 2
2.向下取整 函數(shù)
//2.向下取整
var num1 = Math.floor(1.5);
console.log(num1);
// 1
3.四舍五入
//3.四舍五入
var num2 = Math.round(1.6);
console.log(num2);
// 2