创建日期对象

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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值