Math對象

js提供的一些數學方法

PI屬性不需要加括號
        console.log(Math.PI)

最大值
        console.log(Math.max(12, 5, 7, 99, 103, 1))

最小值
        console.log(Math.min(12, 5, 7, 99, 103, 1))

 冪
         console.log(Math.pow(2, 10)) 2的10次冪

開平方
         console.log(Math.sqrt(10))

向上取整
         console.log(Math.ceil(12.1))

向下取整
         console.log(Math.floor(-12.9))

四捨五入
         console.log(Math.round(-12.6))  // 負數 5進  6舍

絕對值
         console.log(Math.abs(-5))

隨機數

- 0 - 1      0會出現,1永遠不會出現,最大值是0.9999999999999
        // console.log(Math.random())
        // 0 - 10
        // console.log(parseInt(Math.random() * 10))
        // 10 - 20
        // console.log(parseInt(Math.random() * 10 + 10))
        // 5 - 15       最大值-最小值=差  隨機數*差值 + 最小值
        // console.log(parseInt(Math.random() * 10 + 5))

函數方法:

function random(n, m) {

            return parseInt(Math.random() * (m - n) + n)

        }

Date

● js 提供的內置構造函數,專門用來獲取時間的

new Date()

●  new Date() 在不傳遞參數的情況下是默認返回當前時間

var time = new Date()

console.log(time) // 當前時間 Fri Mar 01 2019 13:11:23 GMT+0800 (中國標準時間)●  new Date() 在傳入參數的時候,可以獲取到一個你傳遞進去的時間

var time = new Date(‘2019-03-03 13:11:11’)

console.log(time) // Sun Mar 03 2019 13:11:11 GMT+0800 (中國標準時間)●  new Date() 傳遞的參數有多種情況

a.  傳遞兩個數字,第一個表示年,第二個表示月份

var time = new Date(2019, 00) // 月份從 0 開始計數,0 表示 1月,11 表示 12月

console.log(time) // Tue Jan 01 2019 00:00:00 GMT+0800 (中國標準時間)b.  傳遞三個數字,前兩個不變,第三個表示該月份的第幾天,從 1 到 31

var time = new Date(2019, 00, 05)

console.log(time) // Sat Jan 05 2019 00:00:00 GMT+0800 (中國標準時間)c.  傳遞四個數字,前三個不變,第四個表示當天的幾點,從 0 到 23

var time = new Date(2019, 00, 05, 22)

console.log(time) // Sat Jan 05 2019 22:00:00 GMT+0800 (中國標準時間)d.  傳遞五個數字,前四個不變,第五個表示的是該小時的多少分鐘,從 0 到 59

var time = new Date(2019, 00, 05, 22, 33)

console.log(time) // Sat Jan 05 2019 22:33:00 GMT+0800 (中國標準時間)e.  傳遞六個數字,前五個不變,第六個表示該分鐘的多少秒,從 0 到 59

var time = new Date(2019, 00, 05, 22, 33, 55)

console.log(time) // Sat Jan 05 2019 22:33:55 GMT+0800 (中國標準時間)f.  傳入字符串的形式

console.log(new Date(‘2019’))

// Tue Jan 01 2019 08:00:00 GMT+0800 (中國標準時間)

console.log(new Date(‘2019-02’))

// Fri Feb 01 2019 08:00:00 GMT+0800 (中國標準時間)

console.log(new Date(‘2019-02-03’))

// Sun Feb 03 2019 08:00:00 GMT+0800 (中國標準時間)

console.log(new Date(‘2019-02-03 13:’))

// Sun Feb 03 2019 13:00:00 GMT+0800 (中國標準時間)

console.log(new Date(‘2019-02-03 13:13:’))

// Sun Feb 03 2019 13:13:00 GMT+0800 (中國標準時間)

console.log(new Date(‘2019-02-03 13:13:13’))

// Sun Feb 03 2019 13:13:13 GMT+0800 (中國標準時間)

獲取具體時間

年
            var year = now.getFullYear()
            // 月 0 - 11 需要加一
            var month = now.getMonth() + 1
            // 日
            var day = now.getDate()

            // 周幾
            var week = now.getDay()

            // 時
            var h = now.getHours()
            // 分
            var m = now.getMinutes()
            // 秒
            var s = now.getSeconds()

            // 毫秒 1000毫秒 = 1秒
            var ms = now.getMilliseconds()

每秒進行

//setInterval(tick, 1000)
setInterval(function () {
            // 構造函數
            var now = new Date()        // 當前計算機的時間
            // 年
            var year = now.getFullYear()
            // 月 0 - 11
            var month = now.getMonth() + 1
            // 日
            var day = now.getDate()

            // 周幾
            var week = now.getDay()

            // 時
            var h = now.getHours()
            // 分
            var m = now.getMinutes()
            // 秒
            var s = now.getSeconds()

            // 毫秒 1000毫秒 = 1秒
            var ms = now.getMilliseconds()

            oTxt.innerHTML = year + '年' + month + '月' + day + '日' + ' 周' + week + ' ' + toDou(h) + ':' + toDou(m) + ':' + toDou(s)
        }, 1000)

自定義時間

設置未來時間
        // new Date(年, 月, 日, 時, 分, 秒, 毫秒)
        // var target = new Date(2022, 5, 18, 0, 0, 0, 0)
        // console.log(target)

        // 今天的某個時間
        var target = new Date()
        target.setHours(18, 0, 0, 0)
        console.log(target)

時間差

未來時間
var target = new Date(2022, 9, 1, 0, 0, 0, 0)

// 當前時間

var now = new Date()

// 時間差       毫秒數

// 轉換成秒

var reduce = parseInt((target - now) / 1000)

倒計時

function tick() {
            // 未來時間
            var target = new Date(2022, 9, 1, 0, 0, 0, 0)
            // 當前時間
            var now = new Date()
            // 時間差       毫秒數
            // 轉換成秒
            var reduce = parseInt((target - now) / 1000)

            // 1分鐘 = 60秒
            // 1小時 = 3600秒
            // 1天 = 86400秒

            // 天
            var day = parseInt(reduce / 86400)
            // 時
            var h = parseInt(reduce % 86400 / 3600)
            // 分
            var m = parseInt(reduce % 3600 / 60)
            // 秒
            var s = reduce % 60

            // console.log(day + '天' + h + '小時' + m + '分' + s + '秒')
            oTxt.innerHTML = day + '天' + h + '小時' + m + '分' + s + '秒'
        }
        tick() // 一上來就執行一次

        setInterval(tick, 1000) // 過一秒以後再執行一次

錢幣格式轉換

function mFn(n) {
            // console.log(n)      // 00000001
            // 把數字變成字符串,再變成數組並且是個反序
            var arr = (n + '').split('').reverse()
            // 準備一個新數組
            var newArr = []
            // 循環老數組,到3的倍數多存一個逗號”,“
            for (var i = 0; i < arr.length; i++) {
                newArr.push(arr[i])     // 把每個數字都存到新數字裏面
                if ((i + 1) % 3 === 0) {    // 判斷3的倍數
                    newArr.push(',')
                }
            }
            console.log(newArr)
            // 把新數組反轉過來,變成字符串
            var str = newArr.reverse().join('')
            // console.log(str)
            // 如果第一位是逗號”,“需要把它替換掉
            if (str[0] === ',') {
                str = str.replace(',', '')  // 替換完要重新賦值
            }

            return str
        }
        console.log(mFn(10000000))

雙色球

// 6個不能重複的紅球
        var arr = []

        function random(n, m) {
            return parseInt(Math.random() * (m - n) + n)
        }

        // for有問題,固定次數
        // for (var i = 0; i < 6; i++) {
        //     var n = random(1, 34)

        //     if (arr.indexOf(n) === -1) {
        //         arr.push(n)
        //     }
        // }
        // console.log(arr)

        function toDou(n) {
            return n < 10 ? '0' + n : '' + n
        }

        while (arr.length < 6) {
            // 隨機一個數
            var n = random(1, 34)
            // 判斷數組裏有沒有這個數字,沒有存一下
            if (arr.indexOf(n) === -1) {
                arr.push(n)
            }
        }
        console.log(arr)

        // 獲取這個元素
        // 通過標籤名獲取一組元素
        var aTxt = document.getElementsByTagName('p')
        var oDiv = document.getElementById('div1')

        // 紅球
        for (var i = 0; i < arr.length; i++) {
            aTxt[i].innerHTML = toDou(arr[i])
        }
        // 籃球
        oDiv.innerHTML = toDou(random(1, 17))