获取当前时间前三个小时整点的时间数组

  • Post author:
  • Post category:其他


要获取当前时间的前三个小时的整点,比如当前14:12分,要获取的就是14:10,14:00,13:50…以此类推到11:20

<!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>
	<script src="https://cdn.bootcdn.net/ajax/libs/moment.js/2.27.0/moment.js"></script>
    
</head>
<body>
    <script >

    function timeList (){
        let endDate =new Date();
        let splitTime = 10 * 60 * 1000;
        let count = 18;
        let endTime = endDate.getTime();

        let mod = endTime % splitTime

        if(mod>0){
            endTime = endTime - mod;
        }
        var dateArray = [];
        while(count-->0){
            let d =new Date();
            d.setTime(endTime - count * splitTime);
            dateArray.push(moment(d).format('HH:mm'));
        }
        return dateArray;
    }
    let a = timeList();
    console.log(a.length);
    console.log(a)
    </script>
</body>
</html>

工作中遇到的需求,网上找到的方法,链接:

大神求助一个js问题,获取当前时间一个小时前每隔5分钟的时间数组-CSDN论坛



版权声明:本文为weixin_48653292原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。