码迷,mamicode.com
首页 > 其他好文 > 详细

日期函数(date)

时间:2018-03-01 13:30:35      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:活动   console   ali   date()   开始   doc   fixed   min   mil   

1.声明日期

  var date = new Date();

2.使用函数(从1970年1月1日)

  date.getTime();

  date.valueOf();

  date.now();            返回自1970年1月1日 00:00:00 UTC到当前时间的毫秒数,类型为Number

  new Date();           返回一个日期对象,可以调用getDate(),内容为当前时间

3.常用方法

  getDate()                获取日(1-31)

  getDay()                 获取星期(0-6)

  getMonth()             获取月(0-11)

  getFullYear()          获取完整年份

  getHours()              获取小时(0-23)

  getMinutes()           获取分钟(0-59)

  getSeconds()         获取秒(0-59)

  getMilliseconds()    获取毫秒(当前)

  getTime()                返回累计毫秒数

4.倒计时案例

<style>
#timeShow{color:#fff;padding:10px 20px;margin:0 auto;filter:alpha(opacity=50);position:fixed;top:40%;width:1152px;height:40px;margin:0 auto;text-align:center;line-height:40px;font-size:30px;left:50%;margin-left:-596px;background-color:#000;background-color:rgba(0,0,0,.5);}
</style>
<div id="timeShow"></div>
<script>
        showTime();
        function showTime(){
            var curtime = new Date(),//当前时间
                endtime = new Date("2017/10/30,00:00:00"),//结束时间
                lefttime = parseInt((endtime.getTime() - curtime.getTime()) / 1000),//秒
                // d = parseInt(lefttime/(24*60*60)),
                // h = parseInt(lefttime/(60*60)%24),//%取膜
                h = parseInt(lefttime/(60*60)),
                m = parseInt(lefttime/60%60),
                s = parseInt(lefttime%60);
                document.getElementById(‘timeShow‘).innerHTML =‘距离活动开始还剩:‘+ h+‘小时‘+m+‘分‘+s+‘秒‘;
                if(lefttime<=0){
                    document.getElementById(‘timeShow‘).innerHTML = ‘‘;
                    document.getElementById(‘timeShow‘).style.display="none";
                };
                setTimeout(showTime, 500);
                console.log(lefttime);
            }

</script>

日期函数(date)

标签:活动   console   ali   date()   开始   doc   fixed   min   mil   

原文地址:https://www.cnblogs.com/water-wf/p/8487702.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!