时间(字符串、时间戳、Date)转换

字符串==>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 d1 = '2021-7-14'
        let arry = d1.split('-')
        let date = new Date(arry[0],arry[1]-1,arry[2])
        console.log(date)
        // 方法二
        let d1 = '2021-7-14'
        let arry = d1.split('-')
        let date = new Date()
        date.setFullYear(arry[0])
        date.setMonth(arry[1]-1)
        date.getDate(arry[2])
        console.log(date)

    </script>
</body>
</html>

 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()
        let d = date.toLocaleDateString()
        console.log(d)
        //方法二
        let date = new Date()
        let d = `${date.getFullYear()}-${date.getMonth() +1}-${date.getDate()}`
        console.log(d)

    </script>
</body>
</html>

字符串==>时间戳 

<!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 d1 = '2021-7-14'
        let arry = d1.split('-')
        let date = new Date(arry[0],arry[1],arry[2])
        let s = date.getTime()
        console.log(s)
       
    </script>
</body>
</html>

 时间戳==>字符串

<!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 s = 0
        let date = new Date()
        date.setTime(s)
        console.log(date)
        //方法二
        let s = 0
        let date = new Date()
        date.setTime(s)
        //输出日期
        let d = date.toLocaleDateString()
        //输出时间
        let t = date.toLocaleTimeString()
        console.log(d)
        console.log(t)
       
    </script>
</body>
</html>

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()
        let s = date.getTime()
        console.log(s)

        //方法二
        let s2 = Date.now()
        console.log(s2)


    </script>
</body>
</html>

时间戳==>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 s = 0
        let date = new Date(s)
        console.log(date)
        //方法二
        let s = 0
        let date = new Date()
        date.setTime(s)
        console.log(date)

 
    </script>
</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值