new Date() 獲取當前時間對象(getFullYear、getMonth、getDate、getHours、getMinutes、getSeconds、getDay、getTime)

new Date() 獲取當前時間對象

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>溫故而知“心”</title>
    <style></style>
</head>

<body></body>
<script>
    /*
        new Date() 獲取當前時間對象
            傳遞參數:
                1.不傳參數:返回當前的日期 + 時間 本地時間
                2.傳遞的是數字:(這個數字代表一個時間戳) 返回時間戳對應的時間
                3.傳遞的是字符串 (這個字符串代表的是一個時間)
                    格式:  年-月-日 時:分:秒
                            月(英文) 日,年
                            2020/03/23
    
            時間相關的方法:
                getFullYear()   獲取當前時間的年份  本地時間
                getMonth()      獲取當前時間的月份  (0-11)
                getDate()       獲取當前時間的日    (1-31)  這個月中的第幾天
                getHours()      獲取當前時間的小時數    (1-24)
                getMinutes()    獲取當前時間的分鐘數    (0-59)
                getSeconds()    獲取當前時間的秒鐘數    (0-59)
                getDay()        獲取當前時間在一個星期中是第幾天
                getTime()       獲取當前時間的時間戳
                時間戳指的是距離1970年1月1日8點的毫秒數
    */
   let time = new Date();
   console.log("獲取當前時間的年份  本地時間:", time.getFullYear())
   console.log("獲取當前時間的月份  (0-11):", time.getMonth())
   console.log("獲取當前時間的日    (1-31)  這個月中的第幾天:", time.getDate())
   console.log("獲取當前時間的小時數    (1-24):", time.getHours())
   console.log("獲取當前時間的分鐘數    (0-59):", time.getMinutes())
   console.log("獲取當前時間的秒鐘數    (0-59):", time.getSeconds())
   console.log("獲取當前時間在一個星期中是第幾天:", time.getDay())
   console.log(" 獲取當前時間的時間戳:", time.getTime())

   console.log( new Date("2020-3-23 16:59:07") )
   console.log( new Date("Mar 23,2020") )
   console.log( new Date("2020/03/23") )
</script>

</html>

getTime:返回1970年1月1日到至今的毫秒數,常用於時間戳。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
</head>

<body>
</body>

<script>
    /*
    getTime:返回1970年1月1日到至今的毫秒數,常用於時間戳。

    下面這個時間差可以測試電腦的運行速度
     */
    let firstTime = new Date().getTime();
    for (let i = 0; i < 100000000; i++) {

    }
    let lastTime = new Date().getTime();
    console.log(lastTime - firstTime);
</script>
</html>

封裝函數,打印當前是何年何月何日何時,幾分幾秒。(注意封裝的方法最好通過原型來寫)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
</head>

<body>
</body>

<script>
    /*
    封裝函數,打印當前是何年何月何日何時,幾分幾秒。(注意封裝的方法最好通過原型來寫)
     */
    Date.prototype.getCurrentTime = function () {
        let date = new Date();
        const year = date.getFullYear();//獲取年
        const month = date.getMonth()+1;//獲取月,注意時間是0-11,0代表1月份
        const dates = date.getDate();//獲取日
        const hours = date.getHours();//獲取小時
        const minute = date.getMinutes();//獲取分鐘
        const seconds = date.getSeconds();//獲取秒鐘

        return `${year}${month}${dates}${hours}${minute}${seconds}秒`;
    };
    const date = new Date();
    console.log(date.getCurrentTime());
</script>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章