倒计时方式代码-uniapp,js

uni-app:需导入组件

<template>
	<view>
		<!-- 示例1 -->
		<view>
			<uni-countdown :timer="timer1"></uni-countdown>
		</view>
		<!-- 示例2:绿底白字 -->
		<view style="background:#00B26A; padding:50upx 0;">
			<!-- 割符号颜色splitorColor -->
			<uni-countdown :timer="timer2" borderColor="#FFFFFF" splitor-color="#FFF" fontColor="#FFFFFF" bgrColor="none"></uni-countdown>
		</view>
	</view>
</template>
<script>
	import uniCountdown from "../../components/uni-countdown.vue";
	export default {
		data() {
			// Date 对象用于处理日期和时间
			var dateObj = new Date();
			// getTime() 方法可返回距 1970 年 1 月 1 日之间的毫秒数。
			var currentTime = dateObj.getTime();
			var timer1 = this.formatDateTime((currentTime + 1000 * 2000));
			//2秒   60x60=1h  返回格式为日期或时间的表达式。formatDateTime
			var timer2 = this.formatDateTime((currentTime + 1000 * 3601));    
												
			return {
				title: '倒计时',
				timer1: timer1,
				timer2: timer2,
			}
		},
		components: {
			uniCountdown
		},
		methods: {
			formatDateTime(inputTime) { //时间戳 转 YY-mm-dd HH:ii:ss 
				var date = new Date(inputTime);
				var y = date.getFullYear();
				var m = date.getMonth() + 1;
				m = m < 10 ? ('0' + m) : m;
				var d = date.getDate();
				d = d < 10 ? ('0' + d) : d;
				var h = date.getHours();
				h = h < 10 ? ('0' + h) : h;
				var minute = date.getMinutes();
				var second = date.getSeconds();
				minute = minute < 10 ? ('0' + minute) : minute;
				second = second < 10 ? ('0' + second) : second;
				return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
			}
		},
	}
</script>

<style>
	
	
</style>

原生js写法

【方法1-距离下个时期所剩时间段】

<!doctype html>
<html  lang="en">
<head>
<meta charset="utf-8">
<script type="text/javascript">
    function dayBetween(){
        var today = new Date();
        // var enday = new Date(2019,03,05,0,0,0,0);  //或 new Date("2016/12/31 0:0:0");
		var enday = new Date("2019/3/5 0:0:0");
        var between = enday-today;
       // console.log("between"+between+"\n");
        var sec = Math.floor(between/1000);
        var day = Math.floor((Math.floor((Math.floor(sec/60))/60))/24); 
        var hours = (Math.floor((Math.floor(sec/60))/60))%24;
        var minutes = (Math.floor(sec/60))%60 ;
        var seconds =  sec%60;
       // console.log("result:"+day+"天"+hours+"时"+minutes+"分"+seconds+"秒");
       var t = document.getElementById("time");
       t.innerHTML = "离2019年3月5日0点还剩:"+"<b>"+day+"天"+hours+"时"+minutes+"分"+seconds+"秒"+"</b>";
    }
    window.onload = function(){
      setInterval(dayBetween,1000);
    };
</script>
</head>
<body>
<p id="time"></p>
</body>
</html>

【方法二-1h倒计时】

<html>
<head>
<meta charset="UTF-8">
<title>简单时长倒计时</title>
<SCRIPT type="text/javascript">        
            var maxtime = 60 * 60; //一个小时,按秒计算,自己调整!   
            function CountDown() {
                if (maxtime >= 0) {
                    minutes = Math.floor(maxtime / 60);
                    seconds = Math.floor(maxtime % 60);
                    msg = "距离结束还有" + minutes + "分" + seconds + "秒";
                    document.all["timer"].innerHTML = msg;
                    if (maxtime == 5 * 60)alert("还剩5分钟");
                        --maxtime;
                } else{
                    clearInterval(timer);
                    alert("时间到,结束!");
                }
            }
            timer = setInterval("CountDown()", 1000);                
        </SCRIPT>
</head>
<body>
<div id="timer" style="color:red"></div>
<div id="warring" style="color:red"></div>
</body>
</html>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值