测试有效的js作业之Date

本文介绍了JavaScript中日期的处理,包括日期格式化、计算两个日期相差的天数、获取未来的日期、实现倒计时功能以及创建数码时钟。附带相关代码示例和图片资源。
摘要由CSDN通过智能技术生成
  1. 日期格式化
 <script>
        /* 	 思路:
		日期格式化:
		1.定义一个日期函数:var d = new Date(); 
		2.声明日期函数并定义年月日
		function format(date){
				var y = date.getFullYear();
				var m = date.getMonth()+1;
				
				var d = date.getDate(); 
				3.判定日月的条件
					if(m<10){
					m = '0'+m;
				}
				if(d<10){
					d = '0'+d;
				}
				4.给与返回值
				return y+'|'+m+'|'+d
				5.打印结果
					console.log(format(d))*/
			
			var d = new Date();
			console.log(d);
			
			//2018|12|20
			//第一周 var iF for  function
			function format(date){
				var y = date.getFullYear();
				var m = date.getMonth()+1;
				
				var d = date.getDate();
				if(m<10){
					m = '0'+m;
				}
				if(d<10){
					d = '0'+d;
				}
				
				return y+'|'+m+'|'+d
			}
			
			console.log(format(d))
		
    </script>
  1. 判断两个日期相差的天数
  <script>
/* 思路:
1.定义定义二个日期函数:var d = new Date(); 
		2.声明日期函数并在内部相减,
        3.给与返回值并记死返回值
        4.打印结果*/
        var d1 = new Date();
        var d2 = new Date(1545069606139);

        function getDay(d1, d2) {

            console.log(d1)
            console.log(d2)

            var c = d1 - d2;
            return parseInt((c / 1000 / 60 / 60 / 24))
        }

        console.log(getDay(d1, d2));
    </script>

3.获得N天以后的日期

 <script>
        /* 思路: 
        1.赋值一个以后的函数
        var res = getDateAfter(10)
        2.声明里先创建一个时间,
        3.获取当前时间
        now.setDate(now.getDate()+n)
        4.给与返回值now
        5.打印赋值变量*/
        var res = getDateAfter(10)
			function getDateAfter(n){
				var now = new Date();
				now.setDate(now.getDate()+n)
				return now
			}
			console.log(res)
    </script>

4.倒计时

<style>
        .secKill {
          width:190px;
          height:260px;
          background-image: url("../img/JDTime.png");
          background-size: contain;
          background-position: 50%;
          background-position-x: 50%;
          background-position-y: center;
          background-repeat: no-repeat;
          background-color: red;
        }
        .secKill-title {
          color: white;
          font-size:30px;
          width:100%;
          text-align: center;
          padding-top:31px;
        }
        .secKill-time {
          width:100%;
        }
        .secKill-time-title {
          color: white;
          font-size:20px;
          width:100%;
          text-align: center;
          margin-top:100px;
        }
        .secKill-time-secKill {
          margin-left: auto;
          margin-right: auto;
          width: 130px;
          height: 30px;
          margin-top: 10px;
          display: block;
        }
        .secKill-time-secKill > span:nth-child(-n+2) {
          float:left;
          position: relative;
          width: 30px;
          height: 30px;
          text-align: center;
          background-color: #2f3430;
          margin-right: 20px;
          color: white;
          font-size: 20px;
        }
        .secKill-time-secKill > span:last-child {
          float:left;
          position: relative;
          width: 30px;
          height: 30px;
          text-align: center;
          background-color: #2f3430;
          color: white;
          font-size: 20px;
        }
        </style>
<body>
    <div class="secKill">
        <div class="secKill-title">京东秒杀</div>
        <div class="secKill-time">
          <div class="secKill-time-title">16:00点场倒计时</div>
          <div class="secKill-time-secKill">
            <span>1</span>
            <span>2</span>
            <span>3</span>
          </div>
      </div>
    
      <script>
         /*  思路:1.先做出样式
          2.建立一个时间,随便写,尽量写大点
          3.获取三个时间的元素
          4.获取现在的天数,小时,分钟秒数
          5.小时,分钟,秒数判断
          6.获取小时,分钟,秒数的HTML结构
          7.调用
          8.设置间隔定时 */
        let leastTime = new Date('2020-7-24 10:57:40');
        let spanHour = document.querySelector('.secKill-time-secKill > span:nth-child(1)');
        let spanMinutes = document.querySelector('.secKill-time-secKill > span:nth-child(2)');
        // 封装的倒计时
        let spanSeconds = document.querySelector('.secKill-time-secKill > span:nth-child(3)');
        let updateSecKill = () => {
        
            let nowTime = Date.now();
            let time1 = parseInt((leastTime - nowTime) / 1000);
            let hours = parseInt(time1 / 3600 % 24);
            hours = hours < 10 ? '0' + hours : hours;
            let minutes = parseInt(time1 / 60 % 60);
            minutes = minutes < 10 ? '0' + minutes : minutes;
            let seconds = parseInt(time1 % 60);
            seconds = seconds < 10 ? '0' + seconds : seconds;

            spanHour.innerHTML = hours;
            spanMinutes.innerHTML = minutes;
            spanSeconds.innerHTML = seconds;
            console.log(time1);
           /*  让定时器值为零时停止。
            因为获取的当前时间最小单位是秒数,
            而设置的时间/1000是毫秒数,
            相减后可能会出现负值,所以才设定条件time1<=0 */
            if (time1<=0) {
              clearInterval(a1)
          }
        }
        updateSecKill();
        let a1=setInterval(updateSecKill,1000);
       
      </script> 
</body>

5.数码时钟

<body>
    <img src="./img/0.png" alt="" id="hs">
    <img src="./img/0.png" alt="" id="hg"><img src="./img/0.png" alt="" id="ms">
    <img src="./img/0.png" alt="" id="mg"><img src="./img/0.png" alt="" id="ss">
    <img src="./img/0.png" alt=""id="sg"><script>
      /*   <!-- 1.思路:做好body的内容:六个图片,已存储跳动的数字。
id名,时分秒或:,
2.  先创建一个定时器及里面的新时间
setInterval(function() {
    let now=new Date()
    }, 1000);
3.定义秒数,分钟数,小时数。
 let s=now.getSeconds()
    let m=now.getMinutes()
    let h=now.getHours()
4.判断格个十位,并在控制台中看下是否跳动。 
 if(s<10){
        s='0'+s
    }
    else{
        s=s+''
    }
    console.log('十位'+s[0])//十位
    console.log('个位'+s[1])//个位。
// 5.让内容在页面中跳动.
 // 获取图片地址及元素
    sg.src='./img/'+s[1]+'.png'
    ss.src='./img/'+s[0]+'.png'-->*/


// 先拿到时间
setInterval(function() {
    let now=new Date()
    // console.log(now)
    // 获取秒数,分钟数,小时数。
    let s=now.getSeconds()
    let m=now.getMinutes()
    let h=now.getHours()

    // console.log(s)
    // 分出十位和各位
    if(s<10){
        s='0'+s
    }
    else{
        s=s+''
    }
    console.log('十位'+s[0])//十位
    console.log('个位'+s[1])//个位
    // 获取图片地址及元素
    sg.src='./img/'+s[1]+'.png'
    ss.src='./img/'+s[0]+'.png'
    // 放对应的内容时,记得把它变为字符串。中间放两个+用来拼接。
    if(m<10){
        m='0'+m
    }
    else{
        m=m+''
    }
    console.log('十位'+m[0])//十位
    console.log('个位'+m[1])//个位
    mg.src='./img/'+m[1]+'.png'
    ms.src='./img/'+m[0]+'.png'


    if(h<10){
        h='0'+h
    }
    else{
        h=h+''
    }
    console.log('十位'+h[0])//十位
    console.log('个位'+h[1])//个位
    hg.src='./img/'+h[1]+'.png'
    hs.src='./img/'+h[0]+'.png'
}, 1000);
    </script>

5-1这里是对应的图片,需要的可以下载,使用时一定要跟代码放同一文件夹下,并按0~9大顺序排好。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值