Bootstrap

创建日期对象

1.创建日期对象方法:

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

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

<body>
    <script>
        // 直接new Date(),返回的是当前日期
        let date1 = new Date()
        console.log(date1);

        // 也可以根据指定的日期,创建一个日期对象
        let date2 = new Date('1998-8-8')
        console.log(date2);
    </script>
</body>

</html>

2.也可以根据一个时间戳,创建一个日期对象。时间戳就是:从1970-1-1到指定日期之间的毫秒数:

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

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

<body>
    <script>
        let date = new Date(123456789)
        console.log(date);
    </script>
</body>

</html>

3.getFullYear() 从 Date 对象返回年份:

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

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

<body>
    <script>
        let date = new Date()
        console.log(date.getFullYear());
    </script>
</body>

</html>

4. getMonth() 从 Date 对象返回月份 (0 ~ 11) 注意:0表示1月份,11表示12月份:

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

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

<body>
    <script>
        let date = new Date()
        console.log(date.getMonth()+1);
    </script>
</body>

</html>

5.getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31) 返回的是几号:

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

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

<body>
    <script>
        let date = new Date()
        console.log(date.getDate());
    </script>
</body>

</html>

6.getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)  返回的是周几

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

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

<body>
    <script>
        let date = new Date()
        console.log(date.getDay());
    </script>
</body>

</html>

7.getHours() 返回小时(0-23):

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

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

<body>
    <script>
        let date = new Date()
        console.log(date.getHours());
    </script>
</body>

</html>

8. getMinutes() 返回分钟(0-59):

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

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

<body>
    <script>
        let date = new Date()
        console.log(date.getMinutes());
    </script>
</body>

</html>

9.getSeconds() 返回秒数(0-59)

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

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

<body>
    <script>
        let date = new Date()
        console.log(date.getSeconds());
    </script>
</body>

</html>

10.getMilliseconds() 返回毫秒(0-999)

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

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

<body>
    <script>
        let date = new Date()
        console.log(date.getMilliseconds());
    </script>
</body>

</html>

11.返回一个当前的时间:代码

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

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

<body>
    <script>
        let date = new Date()
        console.log(date.getHours()+":"+date.getMinutes()+":"+date.getSeconds()+" "+date.getMilliseconds());
    </script>
</body>

</html>

;