晴明的博客园 GitHub      CodePen      CodeWars     

[js] 更多的时间处理

#显示到指定时间的剩余时间

            function leftTime() {
                var y = new Date().getFullYear() + 1;
                var nextYear = new Date(y + '/1/1 00:00:00').getTime();
                var rest = (nextYear - (new Date().getTime())) / 1000;
                var day = Math.floor(rest / 86400);
                var hour = Math.floor(rest % 86400 / 3600);
                var minute = Math.floor(rest % 86400 % 3600 / 60);
                var second = Math.floor(rest % 86400 % 3600 % 60);
                setTimeout(arguments.callee, 1000);
                console.log('距今年结束还有' + day + '' + hour + '' + minute + '' + second + '');
                //                return '距今年结束还有' + day + '天' + hour + '时' + minute + '分' + second + '秒';
            }
            setTimeout(leftTime, 1000);

 

 

#

<!DOCTYPE html>
<html>

    <head lang="en">
        <meta charset="UTF-8">
        <title></title>

        <style type="text/css">

        </style>

    </head>

    <body>

    </body>

</html>

<script>
    function GetSunday(n, w) {
        //获取上n周日的时间,周期间隔为w
        var dd = new Date();
        //var dd = new Date(2016,2,20,1,1,1);
        console.log(dd);
        var a = dd.getDay(); //0-6,0是星期日
        a == 0 ? a = 7 : a = a;
        console.log(a);
        var b = dd.getDate() - a - 7 * w * (n - 1);
        //- 7 * w * (n - 1)是为了跳到上w周,-a是为了设置到上周日
        console.log(b);
        var uts = dd.setDate(b);
        //var UnixTimestamp = Math.round(uts / 1000);  //没搞懂为什么一定要除以1000
        console.log(uts); //Unix时间戳
        //console.log(UnixTimestamp); 
        console.log(dd.toLocaleString()); //toxx不改变原日期格式
        console.log(dd);
        //var k = (dd.setDate(dd.getDate() - a - 35 * n)).getFullYear(); !错误
        //Unix时间不可以直接getXX
        var y = dd.getFullYear();
        var m = dd.getMonth() + 1;
        var d = dd.getDate();
        console.log(y + '' + m + '' + d + '');
    }
    //GetSunday(1,1);
    //GetSunday(2,1);
    //GetSunday(1,2);
    //GetSunday(2,2);
    var GetS = function(n, w) {
            var dd = new Date();
            var a = dd.getDay();
            a == 0 ? a = 7 : a = a;
            var b = dd.getDate() - a - 7 * w * (n - 1);
            this.xx = 11;
            //this.uts = dd.setDate(b);
            var y = dd.getFullYear();
            var m = dd.getMonth() + 1;
            var d = dd.getDate();
            //this.sj = y + '年' + m + '月' + d + '日';
            //return y + '年' + m + '月' + d + '日';
            return this.xx;
        }
        //Gets.prototype.aa
        //var abc = new GetS();
        //console.log(abc(1,1).xx);
        //console.log(GetS(1, 1).sj);
    function GetMonday(n, w) {
        //获取上n周一的时间,周期间隔为w
        var dd = new Date();
        var a = dd.getDay();
        a == 0 ? a = 7 : a = a;
        var b = dd.getDate() - a - (7 * w - 1) - 7 * w * (n - 1);
        //- 7 * w * (n - 1)是为了跳到上w周,- a - (7 * w - 1)是为了设置到上w周一
        var uts = dd.setDate(b);
        var y = dd.getFullYear();
        var m = dd.getMonth() + 1;
        var d = dd.getDate();
        console.log(y + '' + m + '' + d + '');
        console.log(uts);
    }
    //GetMonday(1,1);
    //GetMonday(2,1);
    //GetMonday(1,2);
    //GetMonday(2,2);
    //console.log(Date.parse(new Date(Date.UTC(2016,1,1))));
    function GetLastMonthDay() {
        //获取上个月第一天,最后一天
        var a = new Date();
        a.setDate(0);
        var b = a.getDate();//上个月最后一天
        a.setDate(1);
        var c = a.getDate();//上个月第一天
    }
    //GetLastMonthDay();
//    function GetMonthDay() {
//        //获取上n月的时间,周期间隔为w
//        var a = new Date();
//        a.setDate(0);
//        var b = a.getDate();//上个月最后一天
//        a.setDate(1);
//        var c = a.getDate();//上个月第一天
//    }    暂时先写这些
</script>

 

posted @ 2016-02-19 18:44  晴明桑  阅读(151)  评论(0编辑  收藏  举报